Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 16:47:11 GMT
  2. Package jalview.util

File DigestUtils.java

 

Coverage histogram

../../img/srcFileCovDistChart1.png
57% of files have more coverage

Code metrics

24
66
4
1
148
110
20
0.3
16.5
4
5

Classes

Class Line # Actions
DigestUtils 18 66 20
0.074468087.4%
 

Contributing tests

This file is covered by 7 tests. .

Source view

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    * This code copied from Rosetta Code. See
10    * https://rosettacode.org/wiki/MD5/Implementation#Java
11    * https://rosettacode.org/wiki/SHA-256#Implementation_2 Licensed under GNU Free
12    * Document License 1.3 (see end of web page).
13    * https://github.com/meyfa/java-sha256/blob/main/src/main/java/net/meyfa/sha256/Sha256.java
14    * Licensed under MIT. We need a native implementation of MD5 and SHA256 for
15    * JalviewJS which doesn't play well with java.security.MessageDigest or
16    * sun.security.utils.*.
17    */
 
18    public class DigestUtils
19    {
20   
21    static final Charset enc = StandardCharsets.UTF_8;
22   
23    /**
24    * MD5 implementation -- https://rosettacode.org/wiki/MD5/Implementation
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];
 
38  50 toggle 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   
 
44  0 toggle 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   
 
130  0 toggle 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    * SHA256 implementation --
142    * https://github.com/meyfa/java-sha256/blob/main/src/main/java/net/meyfa/sha256/Sha256.java
143    */
 
144  63 toggle public static byte[] computeSHA256(byte[] message)
145    {
146  63 return Sha256.hash(message);
147    }
148    }