| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package net.i2p.crypto.eddsa; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class Utils { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public static int equal(int b, int c) { |
| 29 |
int result = 0; |
| 30 |
int xor = b ^ c; |
| 31 |
for (int i = 0; i < 8; i++) { |
| 32 |
result |= xor >> i; |
| 33 |
} |
| 34 |
return (result ^ 0x01) & 0x01; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static int equal(byte[] b, byte[] c) { |
| 44 |
int result = 0; |
| 45 |
for (int i = 0; i < 32; i++) { |
| 46 |
result |= b[i] ^ c[i]; |
| 47 |
} |
| 48 |
|
| 49 |
return equal(result, 0); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static int negative(int b) { |
| 58 |
return (b >> 8) & 1; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static int bit(byte[] h, int i) { |
| 68 |
return (h[i >> 3] >> (i & 7)) & 1; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static byte[] hexToBytes(String s) { |
| 77 |
int len = s.length(); |
| 78 |
byte[] data = new byte[len / 2]; |
| 79 |
for (int i = 0; i < len; i += 2) { |
| 80 |
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
| 81 |
+ Character.digit(s.charAt(i+1), 16)); |
| 82 |
} |
| 83 |
return data; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static String bytesToHex(byte[] raw) { |
| 92 |
if ( raw == null ) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
final StringBuilder hex = new StringBuilder(2 * raw.length); |
| 96 |
for (final byte b : raw) { |
| 97 |
hex.append(Character.forDigit((b & 0xF0) >> 4, 16)) |
| 98 |
.append(Character.forDigit((b & 0x0F), 16)); |
| 99 |
} |
| 100 |
return hex.toString(); |
| 101 |
} |
| 102 |
|
| 103 |
} |