From 59cf669fa9dc77fe0dee8378650de60795f62c57 Mon Sep 17 00:00:00 2001 From: AlfieDiCaprio <1250155377@qq.com> Date: Tue, 9 May 2023 02:15:55 +0000 Subject: [PATCH] add src/hash/SHA256.java. Signed-off-by: AlfieDiCaprio <1250155377@qq.com> --- src/hash/SHA256.java | 186 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 src/hash/SHA256.java diff --git a/src/hash/SHA256.java b/src/hash/SHA256.java new file mode 100644 index 0000000..b54bf31 --- /dev/null +++ b/src/hash/SHA256.java @@ -0,0 +1,186 @@ +package hash_sign; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.util.Random; + +public class SHA256Util { + + public static int rightrotate(int num, int bits) { + return (num << (32 - bits)) | (num >>> bits); + } + + // Secure Hash Algorithm - 256 + public static int[] sha256(final byte[] rawBytes) { + int[] H = new int[8]; + int a, b, c, d, e, f, g, h, t1, t2; + + // 初始化hash值数组 + H[0] = 0x6a09e667; + H[1] = 0xbb67ae85; + H[2] = 0x3c6ef372; + H[3] = 0xa54ff53a; + H[4] = 0x510e527f; + H[5] = 0x9b05688c; + H[6] = 0x1f83d9ab; + H[7] = 0x5be0cd19; + + // 初始化常数 + final int k[] = new int[] { 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 }; + + final int ognBytesLen = rawBytes.length; + final int ognBitsCount = ognBytesLen * 8; + final int lenMod512 = ognBitsCount % 512; + final int dataBitsCount; + final int dataBytesLen; + // 计算总的Bits数 + if (lenMod512 < 448) { + dataBitsCount = ognBitsCount + 512 - lenMod512; + } else { // lenMod512 >= 448 + dataBitsCount = ognBitsCount + 512 - lenMod512 + 512; + } + dataBytesLen = dataBitsCount / 8; + + byte[] rawData = new byte[dataBytesLen]; + for (int i = 0; i < ognBytesLen; ++i) { + rawData[i] = rawBytes[i]; + } + // 末尾补1000... + rawData[ognBytesLen] = (byte) 0x80; + for (int i = ognBytesLen + 1; i < dataBytesLen - 8; ++i) { + rawData[i] = (byte) 0; + } + // 加上有效数据比特数 + for (int i = dataBytesLen - 8, j = 56; i < dataBytesLen; ++i, j -= 8) { + rawData[i] = (byte) ((long) ognBitsCount >> j); + } + + int w[] = new int[64]; + // 每次处理 512/8=64 字节,共512比特 + for (int pos = 0; pos < dataBytesLen; pos += 64) { + for (int i = 0; i < 16; ++i) { + // 将4个8位数字节放入一个32位整型中 + w[i] = ((rawData[pos + i * 4] & 0xff) << 24) | ((rawData[pos + i * 4 + 1] & 0xff) << 16) + | ((rawData[pos + i * 4 + 2] & 0xff) << 8) | (rawData[pos + i * 4 + 3] & 0xff); + } + + // 扩充w + for (int i = 16; i < 64; ++i) { + w[i] = w[i - 16] + w[i - 7] + + (rightrotate(w[i - 15], 7) ^ rightrotate(w[i - 15], 18) ^ (w[i - 15] >>> 3)) + + (rightrotate(w[i - 2], 17) ^ rightrotate(w[i - 2], 19) ^ (w[i - 2] >>> 10)); + } + + // 初始化 + a = H[0]; + b = H[1]; + c = H[2]; + d = H[3]; + e = H[4]; + f = H[5]; + g = H[6]; + h = H[7]; + + for (int i = 0; i < 64; ++i) { + t1 = h + k[i] + w[i] + (rightrotate(e, 6) ^ rightrotate(e, 11) ^ rightrotate(e, 25)) + + ((e & f) ^ (~e & g)); + t2 = (rightrotate(a, 2) ^ rightrotate(a, 13) ^ rightrotate(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); + + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + + // 累加当前结果 + H[0] = H[0] + a; + H[1] = H[1] + b; + H[2] = H[2] + c; + H[3] = H[3] + d; + H[4] = H[4] + e; + H[5] = H[5] + f; + H[6] = H[6] + g; + H[7] = H[7] + h; + + } + + return H; + } + + public static byte[] ints2bytes(final int[] intCode) { + byte[] byteCode = new byte[intCode.length * 4]; + for (int i = 0; i < intCode.length; ++i) { + byteCode[i * 4] = (byte) ((intCode[i] >>> 24) & 0xff); + byteCode[i * 4 + 1] = (byte) ((intCode[i] >>> 16) & 0xff); + byteCode[i * 4 + 2] = (byte) ((intCode[i] >>> 8) & 0xff); + byteCode[i * 4 + 3] = (byte) (intCode[i] & 0xff); + } + return byteCode; + } + + + + + public static int[] sha256(final String rawStr) { + return sha256(rawStr.getBytes()); + } + + // 获得文件的hash值 + public static int[] sha256File(final String filename) { + int byteCount = 0; + byte[] buff; + + FileInputStream filein; + DataInputStream datain; + + try { + filein = new FileInputStream(filename); + datain = new DataInputStream(filein); + + byteCount = filein.available(); + buff = new byte[byteCount]; + datain.read(buff); + + datain.close(); + filein.close(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.out.println("Fail to load file " + filename); + return null; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.out.println("Fail to load file " + filename); + return null; + } + + return sha256(buff); + } + + public static void main(String[] args) { + + int[] hashVal = sha256File("D:\\Course\\cryptology\\encryption\\Final_EncryptionSystem\\EncryptionSystem\\.classpath"); + System.out.println("Hash code of file" + " is:"+hashVal); + //System.out.println(hashVal); + + } + +} -- Gitee