package com.avepoint.zeus.common.util.hash;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import com.avepoint.zeus.common.util.base64.AveBase64;
public class AveSHA256 {
// Fields
private static byte[] buffer = new byte[0x40];
private static long count;
private static final int[] K = new int[] { 0x428a2f98, 0x71374491,
0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1,
0xefbe4786, 0xfc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa,
0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8,
0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x6ca6351, 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 static int[] stateSHA256 = new int[8];
private static int[] W = new int[0x40];
private static byte[] hashValue;
private static boolean mbDisposed;
private static int state;
// Methods
private static void init() {
stateSHA256 = new int[8];
buffer = new byte[0x40];
W = new int[0x40];
initializeState();
}
private static void initialize() {
initializeState();
buffer = new byte[buffer.length];
W = new int[W.length];
}
private static void initializeState() {
count = 0L;
stateSHA256[0] = 0x6a09e667;
stateSHA256[1] = 0xbb67ae85;
stateSHA256[2] = 0x3c6ef372;
stateSHA256[3] = 0xa54ff53a;
stateSHA256[4] = 0x510e527f;
stateSHA256[5] = 0x9b05688c;
stateSHA256[6] = 0x1f83d9ab;
stateSHA256[7] = 0x5be0cd19;
}
public static String encode(byte[] strSrc) {
// byte[] contents = strSrc.getBytes(Charset.forName("US-ASCII"));
byte[] contents = strSrc;
init();
transformFinalBlock(contents, 0, contents.length);
byte[] con = AveBase64.encode(getHash());
return new String(con, Charset.forName("US-ASCII"));
}
private static byte[] endHash() {
byte[] block = new byte[0x20];
int num = 0x40 - ((int) (count & 0x3fL));
if (num <= 8) {
num += 0x40;
}
byte[] partIn = new byte[num];
partIn[0] = (byte) 0x80;
long num2 = count * 8L;
partIn[num - 8] = (byte) ((num2 >>> 0x38) & 0xffL);
partIn[num - 7] = (byte) ((num2 >>> 0x30) & 0xffL);
partIn[num - 6] = (byte) ((num2 >>> 40) & 0xffL);
partIn[num - 5] = (byte) ((num2 >>> 0x20) & 0xffL);
partIn[num - 4] = (byte) ((num2 >>> 0x18) & 0xffL);
partIn[num - 3] = (byte) ((num2 >>> 0x10) & 0xffL);
partIn[num - 2] = (byte) ((num2 >>> 8) & 0xffL);
partIn[num - 1] = (byte) (num2 & 0xffL);
hashData(partIn, 0, partIn.length);
dwordToBigEndian(block, stateSHA256, 8);
hashValue = block;
return block;
}
private static void hashData(byte[] partIn, int ibStart, int cbSize) {
int countcbSize = cbSize;
int srcOffset = ibStart;
int dstOffset = (int) (count & 0x3fL);
count += countcbSize;
int[] numRef = stateSHA256;
byte[] numRef2 = buffer;
int[] numRef3 = W;
if ((dstOffset > 0) && ((dstOffset + countcbSize) >= 0x40)) {
internalBlockCopy(partIn, srcOffset, buffer, dstOffset,
0x40 - dstOffset);
srcOffset += 0x40 - dstOffset;
countcbSize -= 0x40 - dstOffset;
shaTransform(numRef3, numRef, numRef2);
dstOffset = 0;
}
while (countcbSize >= 0x40) {
internalBlockCopy(partIn, srcOffset, buffer, 0, 0x40);
srcOffset += 0x40;
countcbSize -= 0x40;
shaTransform(numRef3, numRef, numRef2);
}
if (countcbSize > 0) {
internalBlockCopy(partIn, srcOffset, buffer, dstOffset, countcbSize);
}
}
private static int ch(int x, int y, int z) {
return ((x & y) ^ ((x ^ -1) & z));
}
private static void hashCore(byte[] rgb, int ibStart, int cbSize) {
hashData(rgb, ibStart, cbSize);
}
private static byte[] hashFinal() {
return endHash();
}
private static int maj(int x, int y, int z) {
return (((x & y) ^ (x & z)) ^ (y & z));
}
private static int rotateRight(int x, int n) {
return ((x >>> n) | (x << (0x20 - n)));
}
private static void sha256Expand(int[] x) {
for (int i = 0x10; i < 0x40; i++) {
x[i] = ((sigma_1(x[i - 2]) + x[i - 7]) + sigma_0(x[i - 15]))
+ x[i - 0x10];
}
}
private static void shaTransform(int[] expandedBuffer, int[] state,
byte[] block) {
int x = state[0];
int y = state[1];
int z = state[2];
int num4 = state[3];
int num5 = state[4];
int num6 = state[5];
int num8 = state[6];
int num7 = state[7];
dwordFromBigEndian(expandedBuffer, 0x10, block);
sha256Expand(expandedBuffer);
for (int i = 0; i < 0x40; i++) {
int num17 = (((num7 + Sigma_1(num5)) + ch(num5, num6, num8)) + K[i])
+ expandedBuffer[i];
int num13 = num4 + num17;
int num9 = (num17 + Sigma_0(x)) + maj(x, y, z);
i++;
num17 = (((num8 + Sigma_1(num13)) + ch(num13, num5, num6)) + K[i])
+ expandedBuffer[i];
int num14 = z + num17;
int num10 = (num17 + Sigma_0(num9)) + maj(num9, x, y);
i++;
num17 = (((num6 + Sigma_1(num14)) + ch(num14, num13, num5)) + K[i])
+ expandedBuffer[i];
int num16 = y + num17;
int num11 = (num17 + Sigma_0(num10)) + maj(num10, num9, x);
i++;
num17 = (((num5 + Sigma_1(num16)) + ch(num16, num14, num13)) + K[i])
+ expandedBuffer[i];
int num15 = x + num17;
int num12 = (num17 + Sigma_0(num11)) + maj(num11, num10, num9);
i++;
num17 = (((num13 + Sigma_1(num15)) + ch(num15, num16, num14)) + K[i])
+ expandedBuffer[i];
num7 = num9 + num17;
num4 = (num17 + Sigma_0(num12)) + maj(num12, num11, num10);
i++;
num17 = (((num14 + Sigma_1(num7)) + ch(num7, num15, num16)) + K[i])
+ expandedBuffer[i];
num8 = num10 + num17;
z = (num17 + Sigma_0(num4)) + maj(num4, num12, num11);
i++;
num17 = (((num16 + Sigma_1(num8)) + ch(num8, num7, num15)) + K[i])
+ expandedBuffer[i];
num6 = num11 + num17;
y = (num17 + Sigma_0(z)) + maj(z, num4, num12);
i++;
num17 = (((num15 + Sigma_1(num6)) + ch(num6, num8, num7)) + K[i])
+ expandedBuffer[i];
num5 = num12 + num17;
x = (num17 + Sigma_0(y)) + maj(y, z, num4);
}
state[0] += x;
state[1] += y;
state[2] += z;
state[3] += num4;
state[4] += num5;
state[5] += num6;
state[6] += num8;
state[7] += num7;
}
private static int sigma_0(int x) {
return ((rotateRight(x, 7) ^ rotateRight(x, 0x12)) ^ (x >>> 3));
}
private static int Sigma_0(int x) {
return ((rotateRight(x, 2) ^ rotateRight(x, 13)) ^ rotateRight(x, 0x16));
}
private static int sigma_1(int x) {
return ((rotateRight(x, 0x11) ^ rotateRight(x, 0x13)) ^ (x >>> 10));
}
private static int Sigma_1(int x) {
return ((rotateRight(x, 6) ^ rotateRight(x, 11)) ^ rotateRight(x, 0x19));
}
private static void internalBlockCopy(byte[] src, int srcOffset, byte[] dst,
int dstOffset, int count) {
for (; count-- > 0;) {
dst[dstOffset++] = src[srcOffset++];
}
}
public void clear() {
dispose(true);
}
public byte[] computeHash(byte[] buffer) {
if (mbDisposed) {
// throw new ObjectDisposedException(null,
// Environment.GetResourceString("ObjectDisposed_Generic"));
}
if (buffer == null) {
// throw new ArgumentNullException("buffer");
}
hashCore(buffer, 0, buffer.length);
hashValue = hashFinal();
byte[] buffer2 = (byte[]) hashValue.clone();
initialize();
return buffer2;
}
public byte[] computeHash(byte[] buffer, int offset, int count) {
if (mbDisposed) {
// throw new ObjectDisposedException(null,
// Environment.GetResourceString("ObjectDisposed_Generic"));
}
if (buffer == null) {
// throw new ArgumentNullException("buffer");
}
if (offset < 0) {
// throw new ArgumentOutOfRangeException("offset",
// Environment.GetResourceString
// ("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((count < 0) || (count > buffer.length)) {
// throw new ArgumentException(Environment.GetResourceString(
// "Argument_InvalidValue"));
}
if ((buffer.length - count) < offset) {
// throw new ArgumentException(Environment.GetResourceString(
// "Argument_InvalidOffLen"));
}
hashCore(buffer, offset, count);
hashValue = hashFinal();
byte[] buffer2 = (byte[]) hashValue.clone();
initialize();
return buffer2;
}
protected void dispose(boolean disposing) {
if (disposing) {
if (hashValue != null) {
hashValue = new byte[hashValue.length];
// System.Array.Clear(HashValue, 0, HashValue.length);
}
hashValue = null;
mbDisposed = true;
}
}
private int transformBlock(byte[] inputBuffer, int inputOffset,
int inputCount, byte[] outputBuffer, int outputOffset) {
if (mbDisposed) {
// throw new ObjectDisposedException(null,
// Environment.GetResourceString("ObjectDisposed_Generic"));
}
if (inputBuffer == null) {
// throw new ArgumentNullException("inputBuffer");
}
if (inputOffset < 0) {
// throw new ArgumentOutOfRangeException("inputOffset",
// Environment.GetResourceString
// ("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((inputCount < 0) || (inputCount > inputBuffer.length)) {
// throw new ArgumentException(Environment.GetResourceString(
// "Argument_InvalidValue"));
}
if ((inputBuffer.length - inputCount) < inputOffset) {
// throw new ArgumentException(Environment.GetResourceString(
// "Argument_InvalidOffLen"));
}
state = 1;
hashCore(inputBuffer, inputOffset, inputCount);
if ((outputBuffer != null)
&& ((inputBuffer != outputBuffer) || (inputOffset != outputOffset))) {
internalBlockCopy(inputBuffer, inputOffset, outputBuffer,
outputOffset, inputCount);
}
return inputCount;
}
private static byte[] transformFinalBlock(byte[] inputBuffer, int inputOffset,
int inputCount) {
if (mbDisposed) {
// throw new ObjectDisposedException(null,
// Environment.GetResourceString("ObjectDisposed_Generic"));
}
if (inputBuffer == null) {
// throw new ArgumentNullException("inputBuffer");
}
if (inputOffset < 0) {
// throw new ArgumentOutOfRangeException("inputOffset",
// Environment.GetResourceString
// ("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((inputCount < 0) || (inputCount > inputBuffer.length)) {
// throw new ArgumentException(Environment.GetResourceString(
// "Argument_InvalidValue"));
}
if ((inputBuffer.length - inputCount) < inputOffset) {
// throw new ArgumentException(Environment.GetResourceString(
// "Argument_InvalidOffLen"));
}
hashCore(inputBuffer, inputOffset, inputCount);
hashValue = hashFinal();
byte[] dst = new byte[inputCount];
if (inputCount != 0) {
internalBlockCopy(inputBuffer, inputOffset, dst, 0, inputCount);
}
state = 0;
return dst;
}
public static byte[] getHash() {
if (mbDisposed) {
// throw new ObjectDisposedException(null,
// Environment.GetResourceString("ObjectDisposed_Generic"));
}
if (state != 0) {
// throw new CryptographicUnexpectedOperationException(Environment.
// GetResourceString("Cryptography_HashNotYetFinalized"));
}
return (byte[]) hashValue.clone();
}
static void dwordFromBigEndian(int[] x, int digits, byte[] block) {
int index = 0;
for (int i = 0; index < digits; i += 4) {
x[index] = (int) (((((0xff & (int) block[i]) << 0x18) | ((0xff & (int) block[i + 1]) << 0x10)) | ((0xff & (int) block[i + 2]) << 8)) | (0xff & (int) block[i + 3]));
index++;
}
}
static void dwordToBigEndian(byte[] block, int[] x, int digits) {
int index = 0;
for (int i = 0; index < digits; i += 4) {
block[i] = (byte) ((x[index] >>> 0x18) & 0xff);
block[i + 1] = (byte) ((x[index] >>> 0x10) & 0xff);
block[i + 2] = (byte) ((x[index] >>> 8) & 0xff);
block[i + 3] = (byte) (x[index] & 0xff);
index++;
}
}
public static void main(String[] args) {
// String[] a = new String[] { "a", "ab", "dfs", "dsaf", "sdfsa",
// "sdfaid", "sdifhsa", "sodjusha", "koskdjshi",
// "akdsjdiulsjdhqiwu", "akdsjdiulsjdhqiwus",
// "alsdjfosdfisudfaosidfaoisdufaoidsufasidofujjleisdf" };
// for (String s : a) {
// System.out.println(AveSHA256.encode(s));
// }
String fileName = "d:/a.wsp";
try {
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream(fileName)));
byte []b = new byte[in.available()];
System.out.println("available::::"+in.available());
in.readFully(b);
System.out.println(AveSHA256.encode(b));
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}