1 5 package org.h2.security; 6 7 import java.util.Arrays ; 8 9 public class SHA256 { 10 11 13 public byte[] getHashWithSalt(byte[] data, byte[] salt) { 14 byte[] buff = new byte[data.length + salt.length]; 15 System.arraycopy(data, 0, buff, 0, data.length); 16 System.arraycopy(salt, 0, buff, data.length, salt.length); 17 return getHash(buff); 18 } 19 20 public byte[] getKeyPasswordHash(String userName, char[] password) { 21 String user=userName + "@"; 22 byte[] buff = new byte[2*(user.length()+password.length)]; 23 int n=0; 24 for(int i=0; i<user.length(); i++) { 25 char c = user.charAt(i); 26 buff[n++] = (byte) (c >> 8); 27 buff[n++] = (byte) (c); 28 } 29 for(int i=0; i<password.length; i++) { 30 char c = password[i]; 31 buff[n++] = (byte) (c >> 8); 32 buff[n++] = (byte) (c); 33 } 34 Arrays.fill(password, (char)0); 35 return getHash(buff); 36 } 37 38 private static final int[] K = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 41 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 42 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 43 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 44 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 45 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 46 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 47 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 48 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 49 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 50 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 51 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 52 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; 53 54 55 public byte[] getHash(byte[] data) { 56 int bytelen = data.length; 57 int intlen = ((bytelen + 9 + 63) / 64) * 16; 58 byte[] bytes = new byte[intlen * 4]; 59 System.arraycopy(data, 0, bytes, 0, bytelen); 60 bytes[bytelen] = (byte) 0x80; 61 int[] buff = new int[intlen]; 62 for (int i = 0, j = 0; j < intlen; i += 4, j++) { 63 buff[j] = readInt(bytes, i); 64 } 65 buff[intlen - 2] = bytelen >>> 29; 66 buff[intlen - 1] = (bytelen << 3) & 0xffffffff; 67 int[] w = new int[64]; 68 int[] hh = new int[] { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 69 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; 70 71 for (int block = 0; block < intlen; block += 16) { 72 for (int i = 0; i < 16; i++) { 73 w[i] = buff[block + i]; 74 } 75 for (int i = 16; i < 64; i++) { 76 int x = w[i - 2]; 77 int theta1 = rot(x, 17) ^ rot(x, 19) ^ (x >>> 10); 78 x = w[i - 15]; 79 int theta0 = rot(x, 7) ^ rot(x, 18) ^ (x >>> 3); 80 w[i] = theta1 + w[i - 7] + theta0 + w[i - 16]; 81 } 82 83 int a = hh[0], b = hh[1], c = hh[2], d = hh[3]; 84 int e = hh[4], f = hh[5], g = hh[6], h = hh[7]; 85 86 for (int i = 0; i < 64; i++) { 87 int t1 = h + (rot(e, 6) ^ rot(e, 11) ^ rot(e, 25)) 88 + ((e & f) ^ ((~e) & g)) + K[i] + w[i]; 89 int t2 = (rot(a, 2) ^ rot(a, 13) ^ rot(a, 22)) 90 + ((a & b) ^ (a & c) ^ (b & c)); 91 h = g; 92 g = f; 93 f = e; 94 e = d + t1; 95 d = c; 96 c = b; 97 b = a; 98 a = t1 + t2; 99 } 100 hh[0] += a; 101 hh[1] += b; 102 hh[2] += c; 103 hh[3] += d; 104 hh[4] += e; 105 hh[5] += f; 106 hh[6] += g; 107 hh[7] += h; 108 } 109 byte[] result = new byte[32]; 110 for (int i = 0; i < 8; i++) { 111 writeInt(result, i*4, hh[i]); 112 } 113 Arrays.fill(w, 0); 114 Arrays.fill(buff, 0); 115 Arrays.fill(hh, 0); 116 Arrays.fill(bytes, (byte) 0); 117 return result; 118 } 119 120 private int rot(int i, int count) { 121 return (i << (32 - count)) | (i >>> count); 122 } 123 124 private int readInt(byte[] b, int i) { 125 return ((b[i] & 0xff) << 24) + ((b[i + 1] & 0xff) << 16) 126 + ((b[i + 2] & 0xff) << 8) + (b[i + 3] & 0xff); 127 } 128 129 private void writeInt(byte[] b, int i, int value) { 130 b[i] = (byte) (value >> 24); 131 b[i + 1] = (byte) (value >> 16); 132 b[i + 2] = (byte) (value >> 8); 133 b[i + 3] = (byte) value; 134 } 135 136 } 137 | Popular Tags |