1 package org.bouncycastle.util; 2 3 /** 4 * General array utilities. 5 */ 6 public final class Arrays 7 { Arrays()8 private Arrays() 9 { 10 // static class, hide constructor 11 } 12 areEqual( boolean[] a, boolean[] b)13 public static boolean areEqual( 14 boolean[] a, 15 boolean[] b) 16 { 17 if (a == b) 18 { 19 return true; 20 } 21 22 if (a == null || b == null) 23 { 24 return false; 25 } 26 27 if (a.length != b.length) 28 { 29 return false; 30 } 31 32 for (int i = 0; i != a.length; i++) 33 { 34 if (a[i] != b[i]) 35 { 36 return false; 37 } 38 } 39 40 return true; 41 } 42 areEqual( char[] a, char[] b)43 public static boolean areEqual( 44 char[] a, 45 char[] b) 46 { 47 if (a == b) 48 { 49 return true; 50 } 51 52 if (a == null || b == null) 53 { 54 return false; 55 } 56 57 if (a.length != b.length) 58 { 59 return false; 60 } 61 62 for (int i = 0; i != a.length; i++) 63 { 64 if (a[i] != b[i]) 65 { 66 return false; 67 } 68 } 69 70 return true; 71 } 72 areEqual( byte[] a, byte[] b)73 public static boolean areEqual( 74 byte[] a, 75 byte[] b) 76 { 77 if (a == b) 78 { 79 return true; 80 } 81 82 if (a == null || b == null) 83 { 84 return false; 85 } 86 87 if (a.length != b.length) 88 { 89 return false; 90 } 91 92 for (int i = 0; i != a.length; i++) 93 { 94 if (a[i] != b[i]) 95 { 96 return false; 97 } 98 } 99 100 return true; 101 } 102 103 /** 104 * A constant time equals comparison - does not terminate early if 105 * test will fail. 106 * 107 * @param a first array 108 * @param b second array 109 * @return true if arrays equal, false otherwise. 110 */ constantTimeAreEqual( byte[] a, byte[] b)111 public static boolean constantTimeAreEqual( 112 byte[] a, 113 byte[] b) 114 { 115 if (a == b) 116 { 117 return true; 118 } 119 120 if (a == null || b == null) 121 { 122 return false; 123 } 124 125 if (a.length != b.length) 126 { 127 return false; 128 } 129 130 int nonEqual = 0; 131 132 for (int i = 0; i != a.length; i++) 133 { 134 nonEqual |= (a[i] ^ b[i]); 135 } 136 137 return nonEqual == 0; 138 } 139 areEqual( int[] a, int[] b)140 public static boolean areEqual( 141 int[] a, 142 int[] b) 143 { 144 if (a == b) 145 { 146 return true; 147 } 148 149 if (a == null || b == null) 150 { 151 return false; 152 } 153 154 if (a.length != b.length) 155 { 156 return false; 157 } 158 159 for (int i = 0; i != a.length; i++) 160 { 161 if (a[i] != b[i]) 162 { 163 return false; 164 } 165 } 166 167 return true; 168 } 169 fill( byte[] array, byte value)170 public static void fill( 171 byte[] array, 172 byte value) 173 { 174 for (int i = 0; i < array.length; i++) 175 { 176 array[i] = value; 177 } 178 } 179 fill( long[] array, long value)180 public static void fill( 181 long[] array, 182 long value) 183 { 184 for (int i = 0; i < array.length; i++) 185 { 186 array[i] = value; 187 } 188 } 189 fill( short[] array, short value)190 public static void fill( 191 short[] array, 192 short value) 193 { 194 for (int i = 0; i < array.length; i++) 195 { 196 array[i] = value; 197 } 198 } 199 hashCode(byte[] data)200 public static int hashCode(byte[] data) 201 { 202 if (data == null) 203 { 204 return 0; 205 } 206 207 int i = data.length; 208 int hc = i + 1; 209 210 while (--i >= 0) 211 { 212 hc *= 257; 213 hc ^= data[i]; 214 } 215 216 return hc; 217 } 218 clone(byte[] data)219 public static byte[] clone(byte[] data) 220 { 221 if (data == null) 222 { 223 return null; 224 } 225 byte[] copy = new byte[data.length]; 226 227 System.arraycopy(data, 0, copy, 0, data.length); 228 229 return copy; 230 } 231 clone(int[] data)232 public static int[] clone(int[] data) 233 { 234 if (data == null) 235 { 236 return null; 237 } 238 int[] copy = new int[data.length]; 239 240 System.arraycopy(data, 0, copy, 0, data.length); 241 242 return copy; 243 } 244 } 245