Clover icon

Coverage Report

  1. Project Clover database Mon Dec 8 2025 13:36:16 GMT
  2. Package net.meyfa.sha256

File Sha256.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
53
10
1
223
129
18
0.34
5.3
10
1.8

Classes

Class Line # Actions
Sha256 11 53 18
0.9873417698.7%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * copied from https://github.com/meyfa/java-sha256
3    * with adaptations by bsoares to remove java.nio.IntBuffer and java.nio.ByteBuffer as these are not supported by SwingJS
4    */
5   
6    package net.meyfa.sha256;
7   
8    /**
9    * Offers a {@code hash(byte[])} method for hashing messages with SHA-256.
10    */
 
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    * Hashes the given message with SHA-256 and returns the hash.
37    *
38    * @param message
39    * The bytes to hash.
40    * @return The hash's bytes.
41    */
 
42  63 toggle public static byte[] hash(byte[] message)
43    {
44    // working arrays
45  63 final int[] W = new int[64];
46  63 final int[] H = new int[8];
47  63 final int[] TEMP = new int[8];
48   
49    // let H = H0
50  63 System.arraycopy(H0, 0, H, 0, H0.length);
51   
52    // initialize all words
53  63 int[] words = pad(message);
54   
55    // enumerate all blocks (each containing 16 words)
56  126 for (int i = 0, n = words.length / 16; i < n; ++i)
57    {
58   
59    // initialize W from the block's words
60  63 System.arraycopy(words, i * 16, W, 0, 16);
61  3087 for (int t = 16; t < W.length; ++t)
62    {
63  3024 W[t] = smallSig1(W[t - 2]) + W[t - 7] + smallSig0(W[t - 15])
64    + W[t - 16];
65    }
66   
67    // let TEMP = H
68  63 System.arraycopy(H, 0, TEMP, 0, H.length);
69   
70    // operate on TEMP
71  4095 for (int t = 0; t < W.length; ++t)
72    {
73  4032 int t1 = TEMP[7] + bigSig1(TEMP[4]) + ch(TEMP[4], TEMP[5], TEMP[6])
74    + K[t] + W[t];
75  4032 int t2 = bigSig0(TEMP[0]) + maj(TEMP[0], TEMP[1], TEMP[2]);
76  4032 System.arraycopy(TEMP, 0, TEMP, 1, TEMP.length - 1);
77  4032 TEMP[4] += t1;
78  4032 TEMP[0] = t1 + t2;
79    }
80   
81    // add values in TEMP to values in H
82  567 for (int t = 0; t < H.length; ++t)
83    {
84  504 H[t] += TEMP[t];
85    }
86   
87    }
88   
89  63 return toByteArray(H);
90    }
91   
92    /**
93    * <b>Internal method, no need to call.</b> Pads the given message to have a
94    * length that is a multiple of 512 bits (64 bytes), including the addition of
95    * a 1-bit, k 0-bits, and the message length as a 64-bit integer. The result
96    * is a 32-bit integer array with big-endian byte representation.
97    *
98    * @param message
99    * The message to pad.
100    * @return A new array with the padded message bytes.
101    */
 
102  63 toggle public static int[] pad(byte[] message)
103    {
104    // new message length: original + 1-bit and padding + 8-byte length
105    // --> block count: whole blocks + (padding + length rounded up)
106  63 int finalBlockLength = message.length % BLOCK_BYTES;
107  63 int blockCount = message.length / BLOCK_BYTES
108  63 + (finalBlockLength + 1 + 8 > BLOCK_BYTES ? 2 : 1);
109   
110  63 int capacity = blockCount * (BLOCK_BYTES / Integer.BYTES);
111  63 int[] resultArray = new int[capacity];
112    /**
113    * TO BE REMOVED final IntBuffer result = IntBuffer .allocate(blockCount *
114    * (BLOCK_BYTES / Integer.BYTES)); // copy as much of the message as
115    * possible ByteBuffer buf = ByteBuffer.wrap(message);
116    */
117  63 int n = message.length / Integer.BYTES;
118  63 int m = n * Integer.BYTES;
119    // System.out.println("message.length=" + message.length + "\nn=" + n);
120  521 for (int i = 0; i < n; ++i)
121    {
122  458 int j = 4 * i;
123  458 resultArray[i] = bytesToInt(message[j], message[j + 1],
124    message[j + 2], message[j + 3]);
125    /**
126    * TO BE REMOVED result.put(buf.getInt());
127    */
128    }
129    // copy the remaining bytes (less than 4) and append 1 bit (rest is zero)
130    /**
131    * TO BE REMOVED ByteBuffer remainder = ByteBuffer.allocate(4);
132    * remainder.put(buf).put((byte) 0b10000000).rewind();
133    */
134   
135  63 byte[] remainderArray = new byte[] { 0, 0, 0, 0 };
136  77 for (int i = m; i < message.length; i++)
137    {
138  14 remainderArray[i - m] = message[i];
139    }
140  63 remainderArray[message.length - m] = (byte) 0b10000000;
141  63 resultArray[n] = bytesToInt(remainderArray[0], remainderArray[1],
142    remainderArray[2], remainderArray[3]);
143    /**
144    * TO BE REMOVED result.put(remainder.getInt());
145    *
146    * // ignore however many pad bytes (implicitly calculated in the beginning)
147    * result.position(result.capacity() - 2); // place original message length
148    * as 64-bit integer at the end long msgLength = message.length * 8L;
149    * result.put((int) (msgLength >>> 32)); result.put((int) msgLength);
150    */
151   
152  63 long msgLength = message.length * 8L;
153  63 resultArray[capacity - 2] = (int) (msgLength >>> 32);
154  63 resultArray[capacity - 1] = (int) (msgLength);
155  63 return resultArray;
156    /**
157    * TO BE REMOVED // return result.array();
158    */
159    }
160   
 
161  521 toggle private static int bytesToInt(byte b0, byte b1, byte b2, byte b3)
162    {
163  521 return (0x000000ff & (b3 << 0)) | (0x0000ff00 & (b2 << 8))
164    | (0x00ff0000 & (b1 << 16)) | (0xff000000 & (b0 << 24));
165    }
166   
167    /**
168    * Converts the given int array into a byte array via big-endian conversion (1
169    * int becomes 4 bytes).
170    *
171    * @param ints
172    * The source array.
173    * @return The converted array.
174    */
 
175  63 toggle private static byte[] toByteArray(int[] ints)
176    {
177  63 byte[] bufArray = new byte[ints.length * Integer.BYTES];
178  567 for (int i = 0; i < ints.length; i++)
179    {
180  504 int j = i * Integer.BYTES;
181  504 int n = ints[i];
182  504 bufArray[j + 0] = (byte) ((0xff000000 & n) >> 24);
183  504 bufArray[j + 1] = (byte) ((0x00ff0000 & n) >> 16);
184  504 bufArray[j + 2] = (byte) ((0x0000ff00 & n) >> 8);
185  504 bufArray[j + 3] = (byte) ((0x000000ff & n) >> 0);
186    }
187  63 return bufArray;
188    }
189   
 
190  4032 toggle private static int ch(int x, int y, int z)
191    {
192  4032 return (x & y) | ((~x) & z);
193    }
194   
 
195  4032 toggle private static int maj(int x, int y, int z)
196    {
197  4032 return (x & y) | (x & z) | (y & z);
198    }
199   
 
200  4032 toggle private static int bigSig0(int x)
201    {
202  4032 return Integer.rotateRight(x, 2) ^ Integer.rotateRight(x, 13)
203    ^ Integer.rotateRight(x, 22);
204    }
205   
 
206  4032 toggle private static int bigSig1(int x)
207    {
208  4032 return Integer.rotateRight(x, 6) ^ Integer.rotateRight(x, 11)
209    ^ Integer.rotateRight(x, 25);
210    }
211   
 
212  3024 toggle private static int smallSig0(int x)
213    {
214  3024 return Integer.rotateRight(x, 7) ^ Integer.rotateRight(x, 18)
215    ^ (x >>> 3);
216    }
217   
 
218  3024 toggle private static int smallSig1(int x)
219    {
220  3024 return Integer.rotateRight(x, 17) ^ Integer.rotateRight(x, 19)
221    ^ (x >>> 10);
222    }
223    }