Arrays.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.util; 2 3import java.math.BigInteger; 4 5/** 6 * General array utilities. 7 */ 8public final class Arrays 9{ 10 private Arrays() 11 { 12 // static class, hide constructor 13 } 14 15 public static boolean areEqual( 16 boolean[] a, 17 boolean[] b) 18 { 19 if (a == b) 20 { 21 return true; 22 } 23 24 if (a == null || b == null) 25 { 26 return false; 27 } 28 29 if (a.length != b.length) 30 { 31 return false; 32 } 33 34 for (int i = 0; i != a.length; i++) 35 { 36 if (a[i] != b[i]) 37 { 38 return false; 39 } 40 } 41 42 return true; 43 } 44 45 public static boolean areEqual( 46 char[] a, 47 char[] b) 48 { 49 if (a == b) 50 { 51 return true; 52 } 53 54 if (a == null || b == null) 55 { 56 return false; 57 } 58 59 if (a.length != b.length) 60 { 61 return false; 62 } 63 64 for (int i = 0; i != a.length; i++) 65 { 66 if (a[i] != b[i]) 67 { 68 return false; 69 } 70 } 71 72 return true; 73 } 74 75 public static boolean areEqual( 76 byte[] a, 77 byte[] b) 78 { 79 if (a == b) 80 { 81 return true; 82 } 83 84 if (a == null || b == null) 85 { 86 return false; 87 } 88 89 if (a.length != b.length) 90 { 91 return false; 92 } 93 94 for (int i = 0; i != a.length; i++) 95 { 96 if (a[i] != b[i]) 97 { 98 return false; 99 } 100 } 101 102 return true; 103 } 104 105 /** 106 * A constant time equals comparison - does not terminate early if 107 * test will fail. 108 * 109 * @param a first array 110 * @param b second array 111 * @return true if arrays equal, false otherwise. 112 */ 113 public static boolean constantTimeAreEqual( 114 byte[] a, 115 byte[] b) 116 { 117 if (a == b) 118 { 119 return true; 120 } 121 122 if (a == null || b == null) 123 { 124 return false; 125 } 126 127 if (a.length != b.length) 128 { 129 return false; 130 } 131 132 int nonEqual = 0; 133 134 for (int i = 0; i != a.length; i++) 135 { 136 nonEqual |= (a[i] ^ b[i]); 137 } 138 139 return nonEqual == 0; 140 } 141 142 public static boolean areEqual( 143 int[] a, 144 int[] b) 145 { 146 if (a == b) 147 { 148 return true; 149 } 150 151 if (a == null || b == null) 152 { 153 return false; 154 } 155 156 if (a.length != b.length) 157 { 158 return false; 159 } 160 161 for (int i = 0; i != a.length; i++) 162 { 163 if (a[i] != b[i]) 164 { 165 return false; 166 } 167 } 168 169 return true; 170 } 171 172 public static boolean areEqual( 173 long[] a, 174 long[] b) 175 { 176 if (a == b) 177 { 178 return true; 179 } 180 181 if (a == null || b == null) 182 { 183 return false; 184 } 185 186 if (a.length != b.length) 187 { 188 return false; 189 } 190 191 for (int i = 0; i != a.length; i++) 192 { 193 if (a[i] != b[i]) 194 { 195 return false; 196 } 197 } 198 199 return true; 200 } 201 202 public static boolean areEqual( 203 BigInteger[] a, 204 BigInteger[] b) 205 { 206 if (a == b) 207 { 208 return true; 209 } 210 211 if (a == null || b == null) 212 { 213 return false; 214 } 215 216 if (a.length != b.length) 217 { 218 return false; 219 } 220 221 for (int i = 0; i != a.length; i++) 222 { 223 if (!a[i].equals(b[i])) 224 { 225 return false; 226 } 227 } 228 229 return true; 230 } 231 232 public static void fill( 233 byte[] array, 234 byte value) 235 { 236 for (int i = 0; i < array.length; i++) 237 { 238 array[i] = value; 239 } 240 } 241 242 public static void fill( 243 long[] array, 244 long value) 245 { 246 for (int i = 0; i < array.length; i++) 247 { 248 array[i] = value; 249 } 250 } 251 252 public static void fill( 253 short[] array, 254 short value) 255 { 256 for (int i = 0; i < array.length; i++) 257 { 258 array[i] = value; 259 } 260 } 261 262 public static void fill( 263 int[] array, 264 int value) 265 { 266 for (int i = 0; i < array.length; i++) 267 { 268 array[i] = value; 269 } 270 } 271 272 public static int hashCode(byte[] data) 273 { 274 if (data == null) 275 { 276 return 0; 277 } 278 279 int i = data.length; 280 int hc = i + 1; 281 282 while (--i >= 0) 283 { 284 hc *= 257; 285 hc ^= data[i]; 286 } 287 288 return hc; 289 } 290 291 public static int hashCode(char[] data) 292 { 293 if (data == null) 294 { 295 return 0; 296 } 297 298 int i = data.length; 299 int hc = i + 1; 300 301 while (--i >= 0) 302 { 303 hc *= 257; 304 hc ^= data[i]; 305 } 306 307 return hc; 308 } 309 310 public static int hashCode(int[] data) 311 { 312 if (data == null) 313 { 314 return 0; 315 } 316 317 int i = data.length; 318 int hc = i + 1; 319 320 while (--i >= 0) 321 { 322 hc *= 257; 323 hc ^= data[i]; 324 } 325 326 return hc; 327 } 328 329 public static int hashCode(BigInteger[] data) 330 { 331 if (data == null) 332 { 333 return 0; 334 } 335 336 int i = data.length; 337 int hc = i + 1; 338 339 while (--i >= 0) 340 { 341 hc *= 257; 342 hc ^= data[i].hashCode(); 343 } 344 345 return hc; 346 } 347 348 public static byte[] clone(byte[] data) 349 { 350 if (data == null) 351 { 352 return null; 353 } 354 byte[] copy = new byte[data.length]; 355 356 System.arraycopy(data, 0, copy, 0, data.length); 357 358 return copy; 359 } 360 361 public static int[] clone(int[] data) 362 { 363 if (data == null) 364 { 365 return null; 366 } 367 int[] copy = new int[data.length]; 368 369 System.arraycopy(data, 0, copy, 0, data.length); 370 371 return copy; 372 } 373 374 public static BigInteger[] clone(BigInteger[] data) 375 { 376 if (data == null) 377 { 378 return null; 379 } 380 BigInteger[] copy = new BigInteger[data.length]; 381 382 System.arraycopy(data, 0, copy, 0, data.length); 383 384 return copy; 385 } 386 387 public static byte[] copyOf(byte[] data, int newLength) 388 { 389 byte[] tmp = new byte[newLength]; 390 391 if (newLength < data.length) 392 { 393 System.arraycopy(data, 0, tmp, 0, newLength); 394 } 395 else 396 { 397 System.arraycopy(data, 0, tmp, 0, data.length); 398 } 399 400 return tmp; 401 } 402 403 public static int[] copyOf(int[] data, int newLength) 404 { 405 int[] tmp = new int[newLength]; 406 407 if (newLength < data.length) 408 { 409 System.arraycopy(data, 0, tmp, 0, newLength); 410 } 411 else 412 { 413 System.arraycopy(data, 0, tmp, 0, data.length); 414 } 415 416 return tmp; 417 } 418 419 public static long[] copyOf(long[] data, int newLength) 420 { 421 long[] tmp = new long[newLength]; 422 423 if (newLength < data.length) 424 { 425 System.arraycopy(data, 0, tmp, 0, newLength); 426 } 427 else 428 { 429 System.arraycopy(data, 0, tmp, 0, data.length); 430 } 431 432 return tmp; 433 } 434 435 public static BigInteger[] copyOf(BigInteger[] data, int newLength) 436 { 437 BigInteger[] tmp = new BigInteger[newLength]; 438 439 if (newLength < data.length) 440 { 441 System.arraycopy(data, 0, tmp, 0, newLength); 442 } 443 else 444 { 445 System.arraycopy(data, 0, tmp, 0, data.length); 446 } 447 448 return tmp; 449 } 450 451 public static byte[] copyOfRange(byte[] data, int from, int to) 452 { 453 int newLength = getLength(from, to); 454 455 byte[] tmp = new byte[newLength]; 456 457 if (data.length - from < newLength) 458 { 459 System.arraycopy(data, from, tmp, 0, data.length - from); 460 } 461 else 462 { 463 System.arraycopy(data, from, tmp, 0, newLength); 464 } 465 466 return tmp; 467 } 468 469 public static int[] copyOfRange(int[] data, int from, int to) 470 { 471 int newLength = getLength(from, to); 472 473 int[] tmp = new int[newLength]; 474 475 if (data.length - from < newLength) 476 { 477 System.arraycopy(data, from, tmp, 0, data.length - from); 478 } 479 else 480 { 481 System.arraycopy(data, from, tmp, 0, newLength); 482 } 483 484 return tmp; 485 } 486 487 public static long[] copyOfRange(long[] data, int from, int to) 488 { 489 int newLength = getLength(from, to); 490 491 long[] tmp = new long[newLength]; 492 493 if (data.length - from < newLength) 494 { 495 System.arraycopy(data, from, tmp, 0, data.length - from); 496 } 497 else 498 { 499 System.arraycopy(data, from, tmp, 0, newLength); 500 } 501 502 return tmp; 503 } 504 505 public static BigInteger[] copyOfRange(BigInteger[] data, int from, int to) 506 { 507 int newLength = getLength(from, to); 508 509 BigInteger[] tmp = new BigInteger[newLength]; 510 511 if (data.length - from < newLength) 512 { 513 System.arraycopy(data, from, tmp, 0, data.length - from); 514 } 515 else 516 { 517 System.arraycopy(data, from, tmp, 0, newLength); 518 } 519 520 return tmp; 521 } 522 523 private static int getLength(int from, int to) 524 { 525 int newLength = to - from; 526 if (newLength < 0) 527 { 528 throw new IllegalArgumentException(from + " > " + to); 529 } 530 return newLength; 531 } 532} 533