File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\security\SHA256.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.security;
8
9 import java.util.Arrays;
10
11 /**
12 * This class implements the cryptographic hash function SHA-256.
13 */
14 public class SHA256 {
15
16 /**
17 * The first 32 bits of the fractional parts of the cube roots of the first
18 * sixty-four prime numbers.
19 */
20 private static final int[] K = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
21 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98,
22 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
23 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6,
24 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
25 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3,
26 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138,
27 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e,
28 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
29 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
30 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
31 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814,
32 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
33
34 /**
35 * Calculate the hash code by using the given salt. The salt is appended
36 * after the data before the hash code is calculated. After generating the
37 * hash code, the data and all internal buffers are filled with zeros to avoid
38 * keeping insecure data in memory longer than required (and possibly
39 * swapped to disk).
40 *
41 * @param data the data to hash
42 * @param salt the salt to use
43 * @return the hash code
44 */
45 public static byte[] getHashWithSalt(byte[] data, byte[] salt) {
46 byte[] buff = new byte[data.length + salt.length];
47 System.arraycopy(data, 0, buff, 0, data.length);
48 System.arraycopy(salt, 0, buff, data.length, salt.length);
49 return getHash(buff, true);
50 }
51
52 /**
53 * Calculate the hash of a password by prepending the user name and a '@'
54 * character. Both the user name and the password are encoded to a byte
55 * array using UTF-16. After generating the hash code, the password array
56 * and all internal buffers are filled with zeros to avoid keeping the plain text
57 * password in memory longer than required (and possibly swapped to disk).
58 *
59 * @param userName the user name
60 * @param password the password
61 * @return the hash code
62 */
63 public static byte[] getKeyPasswordHash(String userName, char[] password) {
64 String user = userName + "@";
65 byte[] buff = new byte[2 * (user.length() + password.length)];
66 int n = 0;
67 for (int i = 0, length = user.length(); i < length; i++) {
68 char c = user.charAt(i);
69 buff[n++] = (byte) (c >> 8);
70 buff[n++] = (byte) c;
71 }
72 for (char c : password) {
73 buff[n++] = (byte) (c >> 8);
74 buff[n++] = (byte) c;
75 }
76 Arrays.fill(password, (char) 0);
77 return getHash(buff, true);
78 }
79
80 /**
81 * Calculate the hash code for the given data.
82 *
83 * @param data the data to hash
84 * @param nullData if the data should be filled with zeros after calculating
85 * the hash code
86 * @return the hash code
87 */
88 public static byte[] getHash(byte[] data, boolean nullData) {
89 int byteLen = data.length;
90 int intLen = ((byteLen + 9 + 63) / 64) * 16;
91 byte[] bytes = new byte[intLen * 4];
92 System.arraycopy(data, 0, bytes, 0, byteLen);
93 if (nullData) {
94 Arrays.fill(data, (byte) 0);
95 }
96 bytes[byteLen] = (byte) 0x80;
97 int[] buff = new int[intLen];
98 for (int i = 0, j = 0; j < intLen; i += 4, j++) {
99 buff[j] = readInt(bytes, i);
100 }
101 buff[intLen - 2] = byteLen >>> 29;
102 buff[intLen - 1] = byteLen << 3;
103 int[] w = new int[64];
104 int[] hh = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
105 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
106 for (int block = 0; block < intLen; block += 16) {
107 for (int i = 0; i < 16; i++) {
108 w[i] = buff[block + i];
109 }
110 for (int i = 16; i < 64; i++) {
111 int x = w[i - 2];
112 int theta1 = rot(x, 17) ^ rot(x, 19) ^ (x >>> 10);
113 x = w[i - 15];
114 int theta0 = rot(x, 7) ^ rot(x, 18) ^ (x >>> 3);
115 w[i] = theta1 + w[i - 7] + theta0 + w[i - 16];
116 }
117
118 int a = hh[0], b = hh[1], c = hh[2], d = hh[3];
119 int e = hh[4], f = hh[5], g = hh[6], h = hh[7];
120
121 for (int i = 0; i < 64; i++) {
122 int t1 = h + (rot(e, 6) ^ rot(e, 11) ^ rot(e, 25))
123 + ((e & f) ^ ((~e) & g)) + K[i] + w[i];
124 int t2 = (rot(a, 2) ^ rot(a, 13) ^ rot(a, 22))
125 + ((a & b) ^ (a & c) ^ (b & c));
126 h = g;
127 g = f;
128 f = e;
129 e = d + t1;
130 d = c;
131 c = b;
132 b = a;
133 a = t1 + t2;
134 }
135 hh[0] += a;
136 hh[1] += b;
137 hh[2] += c;
138 hh[3] += d;
139 hh[4] += e;
140 hh[5] += f;
141 hh[6] += g;
142 hh[7] += h;
143 }
144 byte[] result = new byte[32];
145 for (int i = 0; i < 8; i++) {
146 writeInt(result, i * 4, hh[i]);
147 }
148 Arrays.fill(w, 0);
149 Arrays.fill(buff, 0);
150 Arrays.fill(hh, 0);
151 Arrays.fill(bytes, (byte) 0);
152 return result;
153 }
154
155 private static int rot(int i, int count) {
156 return (i << (32 - count)) | (i >>> count);
157 }
158
159 private static int readInt(byte[] b, int i) {
160 return ((b[i] & 0xff) << 24) + ((b[i + 1] & 0xff) << 16)
161 + ((b[i + 2] & 0xff) << 8) + (b[i + 3] & 0xff);
162 }
163
164 private static void writeInt(byte[] b, int i, int value) {
165 b[i] = (byte) (value >> 24);
166 b[i + 1] = (byte) (value >> 16);
167 b[i + 2] = (byte) (value >> 8);
168 b[i + 3] = (byte) value;
169 }
170
171 }
172