| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
package net.meyfa.sha256; |
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| |
|
| 98.7% |
Uncovered Elements: 1 (79) |
Complexity: 18 |
Complexity Density: 0.34 |
|
| 11 |
|
public class Sha256 |
| 12 |
|
{ |
| 13 |
|
private static final int[] K = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, |
| 14 |
|
0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 15 |
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, |
| 16 |
|
0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, |
| 17 |
|
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, |
| 18 |
|
0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 19 |
|
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, |
| 20 |
|
0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, |
| 21 |
|
0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, |
| 22 |
|
0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 23 |
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, |
| 24 |
|
0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, |
| 25 |
|
0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, |
| 26 |
|
0xc67178f2 }; |
| 27 |
|
|
| 28 |
|
private static final int[] H0 = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, |
| 29 |
|
0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; |
| 30 |
|
|
| 31 |
|
private static final int BLOCK_BITS = 512; |
| 32 |
|
|
| 33 |
|
private static final int BLOCK_BYTES = BLOCK_BITS / 8; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
@param |
| 39 |
|
|
| 40 |
|
@return |
| 41 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 5 |
Complexity Density: 0.26 |
|
| 42 |
66 |
public static byte[] hash(byte[] message)... |
| 43 |
|
{ |
| 44 |
|
|
| 45 |
66 |
final int[] W = new int[64]; |
| 46 |
66 |
final int[] H = new int[8]; |
| 47 |
66 |
final int[] TEMP = new int[8]; |
| 48 |
|
|
| 49 |
|
|
| 50 |
66 |
System.arraycopy(H0, 0, H, 0, H0.length); |
| 51 |
|
|
| 52 |
|
|
| 53 |
66 |
int[] words = pad(message); |
| 54 |
|
|
| 55 |
|
|
| 56 |
132 |
for (int i = 0, n = words.length / 16; i < n; ++i) |
| 57 |
|
{ |
| 58 |
|
|
| 59 |
|
|
| 60 |
66 |
System.arraycopy(words, i * 16, W, 0, 16); |
| 61 |
3234 |
for (int t = 16; t < W.length; ++t) |
| 62 |
|
{ |
| 63 |
3168 |
W[t] = smallSig1(W[t - 2]) + W[t - 7] + smallSig0(W[t - 15]) |
| 64 |
|
+ W[t - 16]; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
|
| 68 |
66 |
System.arraycopy(H, 0, TEMP, 0, H.length); |
| 69 |
|
|
| 70 |
|
|
| 71 |
4290 |
for (int t = 0; t < W.length; ++t) |
| 72 |
|
{ |
| 73 |
4224 |
int t1 = TEMP[7] + bigSig1(TEMP[4]) + ch(TEMP[4], TEMP[5], TEMP[6]) |
| 74 |
|
+ K[t] + W[t]; |
| 75 |
4224 |
int t2 = bigSig0(TEMP[0]) + maj(TEMP[0], TEMP[1], TEMP[2]); |
| 76 |
4224 |
System.arraycopy(TEMP, 0, TEMP, 1, TEMP.length - 1); |
| 77 |
4224 |
TEMP[4] += t1; |
| 78 |
4224 |
TEMP[0] = t1 + t2; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
594 |
for (int t = 0; t < H.length; ++t) |
| 83 |
|
{ |
| 84 |
528 |
H[t] += TEMP[t]; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
} |
| 88 |
|
|
| 89 |
66 |
return toByteArray(H); |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
@param |
| 99 |
|
|
| 100 |
|
@return |
| 101 |
|
|
| |
|
| 95.8% |
Uncovered Elements: 1 (24) |
Complexity: 4 |
Complexity Density: 0.22 |
|
| 102 |
66 |
public static int[] pad(byte[] message)... |
| 103 |
|
{ |
| 104 |
|
|
| 105 |
|
|
| 106 |
66 |
int finalBlockLength = message.length % BLOCK_BYTES; |
| 107 |
66 |
int blockCount = message.length / BLOCK_BYTES |
| 108 |
66 |
+ (finalBlockLength + 1 + 8 > BLOCK_BYTES ? 2 : 1); |
| 109 |
|
|
| 110 |
66 |
int capacity = blockCount * (BLOCK_BYTES / Integer.BYTES); |
| 111 |
66 |
int[] resultArray = new int[capacity]; |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
66 |
int n = message.length / Integer.BYTES; |
| 118 |
66 |
int m = n * Integer.BYTES; |
| 119 |
|
|
| 120 |
545 |
for (int i = 0; i < n; ++i) |
| 121 |
|
{ |
| 122 |
479 |
int j = 4 * i; |
| 123 |
479 |
resultArray[i] = bytesToInt(message[j], message[j + 1], |
| 124 |
|
message[j + 2], message[j + 3]); |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
66 |
byte[] remainderArray = new byte[] { 0, 0, 0, 0 }; |
| 136 |
80 |
for (int i = m; i < message.length; i++) |
| 137 |
|
{ |
| 138 |
14 |
remainderArray[i - m] = message[i]; |
| 139 |
|
} |
| 140 |
66 |
remainderArray[message.length - m] = (byte) 0b10000000; |
| 141 |
66 |
resultArray[n] = bytesToInt(remainderArray[0], remainderArray[1], |
| 142 |
|
remainderArray[2], remainderArray[3]); |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
66 |
long msgLength = message.length * 8L; |
| 153 |
66 |
resultArray[capacity - 2] = (int) (msgLength >>> 32); |
| 154 |
66 |
resultArray[capacity - 1] = (int) (msgLength); |
| 155 |
66 |
return resultArray; |
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
} |
| 160 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 161 |
545 |
private static int bytesToInt(byte b0, byte b1, byte b2, byte b3)... |
| 162 |
|
{ |
| 163 |
545 |
return (0x000000ff & (b3 << 0)) | (0x0000ff00 & (b2 << 8)) |
| 164 |
|
| (0x00ff0000 & (b1 << 16)) | (0xff000000 & (b0 << 24)); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
@param |
| 172 |
|
|
| 173 |
|
@return |
| 174 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 175 |
66 |
private static byte[] toByteArray(int[] ints)... |
| 176 |
|
{ |
| 177 |
66 |
byte[] bufArray = new byte[ints.length * Integer.BYTES]; |
| 178 |
594 |
for (int i = 0; i < ints.length; i++) |
| 179 |
|
{ |
| 180 |
528 |
int j = i * Integer.BYTES; |
| 181 |
528 |
int n = ints[i]; |
| 182 |
528 |
bufArray[j + 0] = (byte) ((0xff000000 & n) >> 24); |
| 183 |
528 |
bufArray[j + 1] = (byte) ((0x00ff0000 & n) >> 16); |
| 184 |
528 |
bufArray[j + 2] = (byte) ((0x0000ff00 & n) >> 8); |
| 185 |
528 |
bufArray[j + 3] = (byte) ((0x000000ff & n) >> 0); |
| 186 |
|
} |
| 187 |
66 |
return bufArray; |
| 188 |
|
} |
| 189 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 190 |
4224 |
private static int ch(int x, int y, int z)... |
| 191 |
|
{ |
| 192 |
4224 |
return (x & y) | ((~x) & z); |
| 193 |
|
} |
| 194 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 195 |
4224 |
private static int maj(int x, int y, int z)... |
| 196 |
|
{ |
| 197 |
4224 |
return (x & y) | (x & z) | (y & z); |
| 198 |
|
} |
| 199 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 200 |
4224 |
private static int bigSig0(int x)... |
| 201 |
|
{ |
| 202 |
4224 |
return Integer.rotateRight(x, 2) ^ Integer.rotateRight(x, 13) |
| 203 |
|
^ Integer.rotateRight(x, 22); |
| 204 |
|
} |
| 205 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 206 |
4224 |
private static int bigSig1(int x)... |
| 207 |
|
{ |
| 208 |
4224 |
return Integer.rotateRight(x, 6) ^ Integer.rotateRight(x, 11) |
| 209 |
|
^ Integer.rotateRight(x, 25); |
| 210 |
|
} |
| 211 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 212 |
3168 |
private static int smallSig0(int x)... |
| 213 |
|
{ |
| 214 |
3168 |
return Integer.rotateRight(x, 7) ^ Integer.rotateRight(x, 18) |
| 215 |
|
^ (x >>> 3); |
| 216 |
|
} |
| 217 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 218 |
3168 |
private static int smallSig1(int x)... |
| 219 |
|
{ |
| 220 |
3168 |
return Integer.rotateRight(x, 17) ^ Integer.rotateRight(x, 19) |
| 221 |
|
^ (x >>> 10); |
| 222 |
|
} |
| 223 |
|
} |