| 1 |
|
package jalview.util; |
| 2 |
|
|
| 3 |
|
import java.nio.charset.Charset; |
| 4 |
|
import java.nio.charset.StandardCharsets; |
| 5 |
|
|
| 6 |
|
import net.meyfa.sha256.Sha256; |
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| |
|
| 7.4% |
Uncovered Elements: 87 (94) |
Complexity: 20 |
Complexity Density: 0.3 |
|
| 18 |
|
public class DigestUtils |
| 19 |
|
{ |
| 20 |
|
|
| 21 |
|
static final Charset enc = StandardCharsets.UTF_8; |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
private static final int INIT_A = 0x67452301; |
| 27 |
|
|
| 28 |
|
private static final int INIT_B = (int) 0xEFCDAB89L; |
| 29 |
|
|
| 30 |
|
private static final int INIT_C = (int) 0x98BADCFEL; |
| 31 |
|
|
| 32 |
|
private static final int INIT_D = 0x10325476; |
| 33 |
|
|
| 34 |
|
private static final int[] SHIFT_AMTS = { 7, 12, 17, 22, 5, 9, 14, 20, 4, |
| 35 |
|
11, 16, 23, 6, 10, 15, 21 }; |
| 36 |
|
|
| 37 |
|
private static final int[] TABLE_T = new int[64]; |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 38 |
50 |
static... |
| 39 |
|
{ |
| 40 |
3250 |
for (int i = 0; i < 64; i++) |
| 41 |
3200 |
TABLE_T[i] = (int) (long) ((1L << 32) * Math.abs(Math.sin(i + 1))); |
| 42 |
|
} |
| 43 |
|
|
| |
|
| 0% |
Uncovered Elements: 79 (79) |
Complexity: 15 |
Complexity Density: 0.25 |
|
| 44 |
0 |
public static byte[] computeMD5(byte[] message)... |
| 45 |
|
{ |
| 46 |
0 |
int messageLenBytes = message.length; |
| 47 |
0 |
int numBlocks = ((messageLenBytes + 8) >>> 6) + 1; |
| 48 |
0 |
int totalLen = numBlocks << 6; |
| 49 |
0 |
byte[] paddingBytes = new byte[totalLen - messageLenBytes]; |
| 50 |
0 |
paddingBytes[0] = (byte) 0x80; |
| 51 |
|
|
| 52 |
0 |
long messageLenBits = (long) messageLenBytes << 3; |
| 53 |
0 |
for (int i = 0; i < 8; i++) |
| 54 |
|
{ |
| 55 |
0 |
paddingBytes[paddingBytes.length - 8 + i] = (byte) messageLenBits; |
| 56 |
0 |
messageLenBits >>>= 8; |
| 57 |
|
} |
| 58 |
|
|
| 59 |
0 |
int a = INIT_A; |
| 60 |
0 |
int b = INIT_B; |
| 61 |
0 |
int c = INIT_C; |
| 62 |
0 |
int d = INIT_D; |
| 63 |
0 |
int[] buffer = new int[16]; |
| 64 |
0 |
for (int i = 0; i < numBlocks; i++) |
| 65 |
|
{ |
| 66 |
0 |
int index = i << 6; |
| 67 |
0 |
for (int j = 0; j < 64; j++, index++) |
| 68 |
0 |
buffer[j >>> 2] = ((int) ((index < messageLenBytes) ? message[index] |
| 69 |
|
: paddingBytes[index - messageLenBytes]) << 24) |
| 70 |
|
| (buffer[j >>> 2] >>> 8); |
| 71 |
0 |
int originalA = a; |
| 72 |
0 |
int originalB = b; |
| 73 |
0 |
int originalC = c; |
| 74 |
0 |
int originalD = d; |
| 75 |
0 |
for (int j = 0; j < 64; j++) |
| 76 |
|
{ |
| 77 |
0 |
int div16 = j >>> 4; |
| 78 |
0 |
int f = 0; |
| 79 |
0 |
int bufferIndex = j; |
| 80 |
0 |
switch (div16) |
| 81 |
|
{ |
| 82 |
0 |
case 0: |
| 83 |
0 |
f = (b & c) | (~b & d); |
| 84 |
0 |
break; |
| 85 |
|
|
| 86 |
0 |
case 1: |
| 87 |
0 |
f = (b & d) | (c & ~d); |
| 88 |
0 |
bufferIndex = (bufferIndex * 5 + 1) & 0x0F; |
| 89 |
0 |
break; |
| 90 |
|
|
| 91 |
0 |
case 2: |
| 92 |
0 |
f = b ^ c ^ d; |
| 93 |
0 |
bufferIndex = (bufferIndex * 3 + 5) & 0x0F; |
| 94 |
0 |
break; |
| 95 |
|
|
| 96 |
0 |
case 3: |
| 97 |
0 |
f = c ^ (b | ~d); |
| 98 |
0 |
bufferIndex = (bufferIndex * 7) & 0x0F; |
| 99 |
0 |
break; |
| 100 |
|
} |
| 101 |
0 |
int temp = b + Integer.rotateLeft( |
| 102 |
|
a + f + buffer[bufferIndex] + TABLE_T[j], |
| 103 |
|
SHIFT_AMTS[(div16 << 2) | (j & 3)]); |
| 104 |
0 |
a = d; |
| 105 |
0 |
d = c; |
| 106 |
0 |
c = b; |
| 107 |
0 |
b = temp; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
0 |
a += originalA; |
| 111 |
0 |
b += originalB; |
| 112 |
0 |
c += originalC; |
| 113 |
0 |
d += originalD; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
0 |
byte[] md5 = new byte[16]; |
| 117 |
0 |
int count = 0; |
| 118 |
0 |
for (int i = 0; i < 4; i++) |
| 119 |
|
{ |
| 120 |
0 |
int n = (i == 0) ? a : ((i == 1) ? b : ((i == 2) ? c : d)); |
| 121 |
0 |
for (int j = 0; j < 4; j++) |
| 122 |
|
{ |
| 123 |
0 |
md5[count++] = (byte) n; |
| 124 |
0 |
n >>>= 8; |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
0 |
return md5; |
| 128 |
|
} |
| 129 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 130 |
0 |
public static String toHexString(byte[] b)... |
| 131 |
|
{ |
| 132 |
0 |
StringBuilder sb = new StringBuilder(); |
| 133 |
0 |
for (int i = 0; i < b.length; i++) |
| 134 |
|
{ |
| 135 |
0 |
sb.append(String.format("%02X", b[i] & 0xFF)); |
| 136 |
|
} |
| 137 |
0 |
return sb.toString(); |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 144 |
63 |
public static byte[] computeSHA256(byte[] message)... |
| 145 |
|
{ |
| 146 |
63 |
return Sha256.hash(message); |
| 147 |
|
} |
| 148 |
|
} |