Skip to content

Instantly share code, notes, and snippets.

@gingerwizard
Created June 20, 2019 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gingerwizard/ed7e0ee8ab271c1dbb203532d4813d9a to your computer and use it in GitHub Desktop.
Save gingerwizard/ed7e0ee8ab271c1dbb203532d4813d9a to your computer and use it in GitHub Desktop.
int[] HS() {
return new int[] {
(int) 0x6a09e667L,
(int) 0xbb67ae85L,
(int) 0x3c6ef372L,
(int) 0xa54ff53aL,
(int) 0x510e527fL,
(int) 0x9b05688cL,
(int) 0x1f83d9abL,
(int) 0x5be0cd19L
};
}
int[] KS() {
return new int[] {
(int) 0x428a2f98L, (int) 0x71374491L, (int) 0xb5c0fbcfL, (int) 0xe9b5dba5L,
(int) 0x3956c25bL, (int) 0x59f111f1L, (int) 0x923f82a4L, (int) 0xab1c5ed5L,
(int) 0xd807aa98L, (int) 0x12835b01L, (int) 0x243185beL, (int) 0x550c7dc3L,
(int) 0x72be5d74L, (int) 0x80deb1feL, (int) 0x9bdc06a7L, (int) 0xc19bf174L,
(int) 0xe49b69c1L, (int) 0xefbe4786L, (int) 0x0fc19dc6L, (int) 0x240ca1ccL,
(int) 0x2de92c6fL, (int) 0x4a7484aaL, (int) 0x5cb0a9dcL, (int) 0x76f988daL,
(int) 0x983e5152L, (int) 0xa831c66dL, (int) 0xb00327c8L, (int) 0xbf597fc7L,
(int) 0xc6e00bf3L, (int) 0xd5a79147L, (int) 0x06ca6351L, (int) 0x14292967L,
(int) 0x27b70a85L, (int) 0x2e1b2138L, (int) 0x4d2c6dfcL, (int) 0x53380d13L,
(int) 0x650a7354L, (int) 0x766a0abbL, (int) 0x81c2c92eL, (int) 0x92722c85L,
(int) 0xa2bfe8a1L, (int) 0xa81a664bL, (int) 0xc24b8b70L, (int) 0xc76c51a3L,
(int) 0xd192e819L, (int) 0xd6990624L, (int) 0xf40e3585L, (int) 0x106aa070L,
(int) 0x19a4c116L, (int) 0x1e376c08L, (int) 0x2748774cL, (int) 0x34b0bcb5L,
(int) 0x391c0cb3L, (int) 0x4ed8aa4aL, (int) 0x5b9cca4fL, (int) 0x682e6ff3L,
(int) 0x748f82eeL, (int) 0x78a5636fL, (int) 0x84c87814L, (int) 0x8cc70208L,
(int) 0x90befffaL, (int) 0xa4506cebL, (int) 0xbef9a3f7L, (int) 0xc67178f2L
};
}
byte[] digest(byte[] data, byte[] block, int[] words) {
byte[] padded = padMessage(data);
int[] hs = HS();
for (int i = 0; i < padded.length / 64; ++i) {
int[] registers = new int[8];
for (int k = 0; k < 8; ++k)
registers[k] = hs[k];
System.arraycopy(padded, 64 * i, block, 0, 64);
setupWords(block, words);
int[] ks = KS();
for (int j = 0; j < 64; ++j) {
iterate(registers, words, j, ks);
}
for (int j = 0; j < 8; ++j) {
hs[j] += registers[j];
}
}
byte[] hash = new byte[32];
for (int i = 0; i < 8; i++) {
System.arraycopy(intToBytes(hs[i]), 0, hash, 4 * i, 4);
}
return hash;
}
void setupWords(byte[] block, int[] words) {
for (int j = 0; j < 16; j++) {
words[j] = 0;
for (int m = 0; m < 4; m++) {
words[j] |= ((block[j * 4 + m] & 255) << (24 - m * 8));
}
}
for (int j = 16; j < 64; ++j) {
int s0 = Integer.rotateRight(words[j - 15], 7) ^
Integer.rotateRight(words[j - 15], 18) ^
(words[j - 15] >>> 3);
int s1 = Integer.rotateRight(words[j - 2], 17) ^
Integer.rotateRight(words[j - 2], 19) ^
(words[j - 2] >>> 10);
words[j] = words[j - 16] + s0 + words[j - 7] + s1;
}
}
void iterate(int[] registers, int[] words, int j, int[] ks) {
int S0 = Integer.rotateRight(registers[0], 2) ^
Integer.rotateRight(registers[0], 13) ^
Integer.rotateRight(registers[0], 22);
int maj = (registers[0] & registers[1]) ^ (registers[0] & registers[2]) ^ (registers[1] & registers[2]);
int temp2 = S0 + maj;
int S1 = Integer.rotateRight(registers[4], 6) ^
Integer.rotateRight(registers[4], 11) ^
Integer.rotateRight(registers[4], 25);
int ch = (registers[4] & registers[5]) ^ (~registers[4] & registers[6]);
int temp1 = registers[7] + S1 + ch + ks[j] + words[j];
registers[7] = registers[6];
registers[6] = registers[5];
registers[5] = registers[4];
registers[4] = registers[3] + temp1;
registers[3] = registers[2];
registers[2] = registers[1];
registers[1] = registers[0];
registers[0] = temp1 + temp2;
}
byte[] padMessage(byte[] data) {
int length = data.length;
int tail = length % 64;
int padding;
if ((64 - tail >= 9)) {
padding = 64 - tail;
} else {
padding = 128 - tail;
}
byte[] pad = new byte[padding];
pad[0] = (byte) 128;
long bits = length * 8;
for (int i = 0; i < 8; i++) {
pad[pad.length - 1 - i] = (byte) ((bits >>> (8 * i)) & 255);
}
byte[] output = new byte[length + padding];
System.arraycopy(data, 0, output, 0, length);
System.arraycopy(pad, 0, output, length, pad.length);
return output;
}
byte[] intToBytes(int i) {
byte[] b = new byte[4];
for (int c = 0; c < 4; c++) {
b[c] = (byte) ((i >>> (56 - 8 * c)) & 255);
}
return b;
}
String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789abcdef".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 255;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 15];
}
return String.copyValueOf(hexChars);
}
byte[] charArrayToByteArray(char[] charArray) {
byte[] byteArray = new byte[charArray.length];
for(int i= 0; i < charArray.length; i++) {
byteArray[i] = (byte)(255 & (int)charArray[i]);
}
return byteArray;
}
// Main
byte[] block = new byte[64];
int[] words = new int[64];
byte[] data = charArrayToByteArray(ctx.kubernetes.pod.name.toCharArray());
byte[] res = digest(data, block, words);
ctx.kubernetes.pod.uid = bytesToHex(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment