Skip to content

Instantly share code, notes, and snippets.

@upsuper
Last active July 15, 2022 03:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save upsuper/3012171 to your computer and use it in GitHub Desktop.
Save upsuper/3012171 to your computer and use it in GitHub Desktop.
A Java implementation of SHA-224 message digest algorithm with Provider
Copyright (c) 2012 Xidorn Quan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
package org.upsuper.crypto;
import java.security.Provider;
public final class MyCrypto extends Provider {
private static final long serialVersionUID = -2599394895077747264L;
public MyCrypto() {
super("MyCrypto", 0.1, "MyCrypto v0.1, implementing SHA-224");
put("MessageDigest.SHA-224", "org.upsuper.crypto.SHA224");
}
}
package org.upsuper.crypto;
import java.nio.ByteBuffer;
import java.security.MessageDigestSpi;
/**
* SHA-224
*
* @author upsuper
* @see http://en.wikipedia.org/wiki/SHA-224#Examples_of_SHA-2_variants
*/
public final class SHA224 extends MessageDigestSpi {
private static int[] H = {
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
};
private static int[] K = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
private ByteBuffer dataBuf = ByteBuffer.allocate(64);
private int length = 0;
private int[] h = H.clone();
private void processChunk() {
dataBuf.rewind();
int[] w = new int[64];
for (int i = 0; i < 16; i++)
w[i] = dataBuf.getInt();
dataBuf.clear();
for (int i = 16; i < 64; i++) {
int s0 = w[i - 15];
s0 = ((s0 >>> 7) | (s0 << 25)) ^ ((s0 >>> 18) | (s0 << 14)) ^ (s0 >>> 3);
int s1 = w[i - 2];
s1 = ((s1 >>> 17) | (s1 << 15)) ^ ((s1 >>> 19) | (s1 << 13)) ^ (s1 >>> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
int a = this.h[0], b = this.h[1], c = this.h[2], d = this.h[3],
e = this.h[4], f = this.h[5], g = this.h[6], h = this.h[7];
for (int i = 0; i < 64; i++) {
int s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
int maj = (a & b) ^ (a & c) ^ (b & c);
int t2 = s0 + maj;
int s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
int ch = (e & f) ^ ((~e) & g);
int t1 = h + s1 + ch + K[i] + w[i];
h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
this.h[0] += a;
this.h[1] += b;
this.h[2] += c;
this.h[3] += d;
this.h[4] += e;
this.h[5] += f;
this.h[6] += g;
this.h[7] += h;
}
@Override
protected byte[] engineDigest() {
long bitLength = length * 8L;
engineUpdate((byte) 0x80);
for (; (length + 8) % 64 != 0;)
engineUpdate((byte) 0);
dataBuf.putLong(bitLength);
processChunk();
ByteBuffer result = ByteBuffer.allocate(28);
for (int i = 0; i < 7; i++)
result.putInt(h[i]);
engineReset();
return result.array();
}
@Override
protected void engineReset() {
dataBuf.clear();
length = 0;
h = H.clone();
}
@Override
protected void engineUpdate(byte src) {
dataBuf.put(src);
length += 1;
if (length % 64 == 0)
processChunk();
}
@Override
protected void engineUpdate(byte[] src, int offset, int count) {
for (int i = offset; i < offset + count; i++)
engineUpdate(src[i]);
}
}
package org.upsuper.crypto;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
public class SHA224Test {
/**
* SHA-224 Test
*
* @see http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA224.pdf
*/
public static void main(String[] args) {
System.out.println("Hello");
Security.addProvider(new MyCrypto());
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-224", "MyCrypto");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return;
} catch (NoSuchProviderException e) {
e.printStackTrace();
return;
}
byte[] result;
result = digest.digest("abc".getBytes());
for (int i = 0; i < result.length; i++) {
System.out.printf("%02x", result[i]);
if (i % 4 == 3)
System.out.print(" ");
}
System.out.println();
result = digest.digest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".getBytes());
for (int i = 0; i < result.length; i++) {
System.out.printf("%02x", result[i]);
if (i % 4 == 3)
System.out.print(" ");
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment