From 4f159c68bef43d8937108dcbceea01f9738e6aab Mon Sep 17 00:00:00 2001 From: Xiao X Date: Wed, 27 May 2026 21:33:31 -0400 Subject: [PATCH] SHA1 fully tested + working --- src/de/caydenno1/xacrypto/XACrypto.java | 7 +- src/de/caydenno1/xacrypto/hash/sha/SHA0.java | 47 ++--------- src/de/caydenno1/xacrypto/hash/sha/SHA1.java | 82 +++++++++++++++++++ .../caydenno1/xacrypto/hash/sha/Shared.java | 42 ++++++++++ src/de/caydenno1/xacrypto/misc/Constants.java | 2 +- 5 files changed, 132 insertions(+), 48 deletions(-) create mode 100644 src/de/caydenno1/xacrypto/hash/sha/SHA1.java create mode 100644 src/de/caydenno1/xacrypto/hash/sha/Shared.java diff --git a/src/de/caydenno1/xacrypto/XACrypto.java b/src/de/caydenno1/xacrypto/XACrypto.java index f43708f..c8c74f8 100644 --- a/src/de/caydenno1/xacrypto/XACrypto.java +++ b/src/de/caydenno1/xacrypto/XACrypto.java @@ -1,10 +1,7 @@ package de.caydenno1.xacrypto; -import de.caydenno1.xacrypto.hash.sha.SHA0; + public class XACrypto { private XACrypto() {} - static void main(String[] args) { - String res = SHA0.hash("tah"); - System.out.println(res); - } + static void main(String[] args) {} } diff --git a/src/de/caydenno1/xacrypto/hash/sha/SHA0.java b/src/de/caydenno1/xacrypto/hash/sha/SHA0.java index c9e3f29..a0a41f1 100644 --- a/src/de/caydenno1/xacrypto/hash/sha/SHA0.java +++ b/src/de/caydenno1/xacrypto/hash/sha/SHA0.java @@ -4,13 +4,16 @@ import de.caydenno1.xacrypto.misc.Constants; import java.nio.charset.StandardCharsets; +import static de.caydenno1.xacrypto.hash.sha.Shared.INT2BYTE; +import static de.caydenno1.xacrypto.hash.sha.Shared.hex; + public class SHA0 { // we really only need one file. very simple code public static byte[] hash(byte[] data){ System.out.println("Beware, SHA0 is deprecated and cryptographically broken. Use at your own risk."); - byte[] padded = pad(data); + byte[] padded = Shared.pad(data); - int a0 = Constants.SHA0_H[0], a1 = Constants.SHA0_H[1], a2 = Constants.SHA0_H[2], a3 = Constants.SHA0_H[3], a4 = Constants.SHA0_H[4]; + int a0 = Constants.SHA_H[0], a1 = Constants.SHA_H[1], a2 = Constants.SHA_H[2], a3 = Constants.SHA_H[3], a4 = Constants.SHA_H[4]; int[] w = new int[80]; @@ -69,48 +72,8 @@ public class SHA0 { return INT2BYTE(a0, a1, a2, a3, a4); } - public static String hash(String data) { byte[] result = hash(data.getBytes(StandardCharsets.UTF_8)); return hex(result); } - - public static String hex(byte[] data) { - StringBuilder builder = new StringBuilder(); - for (byte b : data) builder.append(String.format("%02x", b)); - return builder.toString(); - } - - private static byte[] pad(byte[] mesg) { - int len = mesg.length; - long bit = (long) len * 8; - - int padL = (64 - ((len + 9) % 64)) % 64; - byte[] o = new byte[len+padL+9]; - - System.arraycopy(mesg,0,o,0,len); - o[len] = (byte) 0x80; - for (int i = 0; i < 8; i++) { - o[o.length - (i+1)] = (byte) (bit >>> (8*i)); - } - - return o; - } - - private static byte[] INT2BYTE(int a,int b,int c,int d,int e) { - byte[] out = new byte[20]; - write(a, out, 0); - write(b, out, 4); - write(c, out, 8); - write(d, out, 12); - write(e, out, 16); - return out; - } - - private static void write(int v, byte[] o, int i) { - o[i ] = (byte)(v >>> 24); - o[i+1] = (byte)(v >>> 16); - o[i+2] = (byte)(v >>> 8); - o[i+3] = (byte)(v ); - } } \ No newline at end of file diff --git a/src/de/caydenno1/xacrypto/hash/sha/SHA1.java b/src/de/caydenno1/xacrypto/hash/sha/SHA1.java new file mode 100644 index 0000000..41c1dd8 --- /dev/null +++ b/src/de/caydenno1/xacrypto/hash/sha/SHA1.java @@ -0,0 +1,82 @@ +package de.caydenno1.xacrypto.hash.sha; + +import de.caydenno1.xacrypto.misc.Constants; +import de.caydenno1.xacrypto.hash.sha.Shared; +import java.nio.charset.StandardCharsets; + +import static de.caydenno1.xacrypto.hash.sha.Shared.hex; + +public class SHA1 { + + public static byte[] hash(byte[] data){ + byte[] padded = Shared.pad(data); + + int a0 = Constants.SHA_H[0], a1 = Constants.SHA_H[1], a2 = Constants.SHA_H[2], a3 = Constants.SHA_H[3], a4 = Constants.SHA_H[4]; + + int[] w = new int[80]; + + for (int i = 0 ; i < padded.length ; i += 64) { + + for (int j = 0 ; j < 16 ; j++) { + int o = i + j * 4; + w[j] = ((padded[o] & 0xff) << 24) + | ((padded[o + 1] & 0xff) << 16) + | ((padded[o + 2] & 0xff) << 8) + | (padded[o + 3] & 0xff); + } + + for (int t = 16; t < 80; t++) { + w[t] = de.caydenno1.xacrypto.hash.ROT.ROTL( + w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16], 1 + ); + } + + int a = a0, b = a1, c = a2, d = a3, e = a4; + + for (int t = 0 ; t < 80 ; t++) { + int f, k; + + if (t < 20) { + f = (b & c) | ((~b) & d); + k = Constants.SHA1_K[0]; + } else if (t < 40) { + f = b ^ c ^ d; + k = Constants.SHA1_K[1]; + } else if (t < 60) { + f = (b & c) | (b & d) | (c & d); + k = Constants.SHA1_K[2]; + } else { + f = b ^ c ^ d; + k = Constants.SHA1_K[3]; + } + + int temp = de.caydenno1.xacrypto.hash.ROT.ROTL(a, 5); + temp += f; + temp += e; + temp += k; + temp += w[t]; + + e = d; + d = c; + c = de.caydenno1.xacrypto.hash.ROT.ROTL(b, 30); + b = a; + a = temp; + } + + a0 += a; + a1 += b; + a2 += c; + a3 += d; + a4 += e; + } + + return Shared.INT2BYTE(a0, a1, a2, a3, a4); + } + + public static String hash(String data) { + byte[] result = hash(data.getBytes(StandardCharsets.UTF_8)); + return hex(result); + } + + +} \ No newline at end of file diff --git a/src/de/caydenno1/xacrypto/hash/sha/Shared.java b/src/de/caydenno1/xacrypto/hash/sha/Shared.java new file mode 100644 index 0000000..92e9368 --- /dev/null +++ b/src/de/caydenno1/xacrypto/hash/sha/Shared.java @@ -0,0 +1,42 @@ +package de.caydenno1.xacrypto.hash.sha; + +public class Shared { + public static byte[] INT2BYTE(int a,int b,int c,int d,int e) { + byte[] out = new byte[20]; + write(a, out, 0); + write(b, out, 4); + write(c, out, 8); + write(d, out, 12); + write(e, out, 16); + return out; + } + + public static void write(int v, byte[] o, int i) { + o[i ] = (byte)(v >>> 24); + o[i+1] = (byte)(v >>> 16); + o[i+2] = (byte)(v >>> 8); + o[i+3] = (byte)(v ); + } + + public static byte[] pad(byte[] mesg) { + int len = mesg.length; + long bit = (long) len * 8; + + int padL = (64 - ((len + 9) % 64)) % 64; + byte[] o = new byte[len+padL+9]; + + System.arraycopy(mesg,0,o,0,len); + o[len] = (byte) 0x80; + for (int i = 0; i < 8; i++) { + o[o.length - (i+1)] = (byte) (bit >>> (8*i)); + } + + return o; + } + + public static String hex(byte[] data) { + StringBuilder builder = new StringBuilder(); + for (byte b : data) builder.append(String.format("%02x", b)); + return builder.toString(); + } +} diff --git a/src/de/caydenno1/xacrypto/misc/Constants.java b/src/de/caydenno1/xacrypto/misc/Constants.java index 2a75dc4..36dd26d 100644 --- a/src/de/caydenno1/xacrypto/misc/Constants.java +++ b/src/de/caydenno1/xacrypto/misc/Constants.java @@ -31,7 +31,7 @@ public class Constants { 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 }; - public static final int[] SHA0_H = { + public static final int[] SHA_H = { 0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0 };