Debian Codegraph @

src/main/org/h2/security/SHA256.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.security;
7
8 import java.security.GeneralSecurityException;
9 import java.security.MessageDigest;
10 import java.security.NoSuchAlgorithmException;
11 import java.util.Arrays;
12
13 import javax.crypto.Mac;
14 import javax.crypto.spec.SecretKeySpec;
15
16 import org.h2.util.Bits;
17
18 /**
19 * This class implements the cryptographic hash function SHA-256.
20 */
21 public class SHA256 {
22
23 private SHA256() {
24 }
25
26 /**
27 * Calculate the hash code by using the given salt. The salt is appended
28 * after the data before the hash code is calculated. After generating the
29 * hash code, the data and all internal buffers are filled with zeros to
30 * avoid keeping insecure data in memory longer than required (and possibly
31 * swapped to disk).
32 *
33 * @param data the data to hash
34 * @param salt the salt to use
35 * @return the hash code
36 */
37 public static byte[] getHashWithSalt(byte[] data, byte[] salt) {
38 byte[] buff = new byte[data.length + salt.length];
39 System.arraycopy(data, 0, buff, 0, data.length);
40 System.arraycopy(salt, 0, buff, data.length, salt.length);
41 return getHash(buff, true);
42 }
43
44 /**
45 * Calculate the hash of a password by prepending the user name and a '@'
46 * character. Both the user name and the password are encoded to a byte
47 * array using UTF-16. After generating the hash code, the password array
48 * and all internal buffers are filled with zeros to avoid keeping the plain
49 * text password in memory longer than required (and possibly swapped to
50 * disk).
51 *
52 * @param userName the user name
53 * @param password the password
54 * @return the hash code
55 */
56 public static byte[] getKeyPasswordHash(String userName, char[] password) {
57 String user = userName + "@";
58 byte[] buff = new byte[2 * (user.length() + password.length)];
59 int n = 0;
60 for (int i = 0, length = user.length(); i < length; i++) {
61 char c = user.charAt(i);
62 buff[n++] = (byte) (c >> 8);
63 buff[n++] = (byte) c;
64 }
65 for (char c : password) {
66 buff[n++] = (byte) (c >> 8);
67 buff[n++] = (byte) c;
68 }
69 Arrays.fill(password, (char) 0);
70 return getHash(buff, true);
71 }
72
73 /**
74 * Calculate the hash-based message authentication code.
75 *
76 * @param key the key
77 * @param message the message
78 * @return the hash
79 */
80 public static byte[] getHMAC(byte[] key, byte[] message) {
81 return initMac(key).doFinal(message);
82 }
83
84 private static Mac initMac(byte[] key) {
85 // Java forbids empty keys
86 if (key.length == 0) {
87 key = new byte[1];
88 }
89 try {
90 Mac mac = Mac.getInstance("HmacSHA256");
91 mac.init(new SecretKeySpec(key, "HmacSHA256"));
92 return mac;
93 } catch (GeneralSecurityException e) {
94 throw new RuntimeException(e);
95 }
96 }
97
98 /**
99 * Calculate the hash using the password-based key derivation function 2.
100 *
101 * @param password the password
102 * @param salt the salt
103 * @param iterations the number of iterations
104 * @param resultLen the number of bytes in the result
105 * @return the result
106 */
107 public static byte[] getPBKDF2(byte[] password, byte[] salt,
108 int iterations, int resultLen) {
109 byte[] result = new byte[resultLen];
110 Mac mac = initMac(password);
111 int len = 64 + Math.max(32, salt.length + 4);
112 byte[] message = new byte[len];
113 byte[] macRes = null;
114 for (int k = 1, offset = 0; offset < resultLen; k++, offset += 32) {
115 for (int i = 0; i < iterations; i++) {
116 if (i == 0) {
117 System.arraycopy(salt, 0, message, 0, salt.length);
118 Bits.writeInt(message, salt.length, k);
119 len = salt.length + 4;
120 } else {
121 System.arraycopy(macRes, 0, message, 0, 32);
122 len = 32;
123 }
124 mac.update(message, 0, len);
125 macRes = mac.doFinal();
126 for (int j = 0; j < 32 && j + offset < resultLen; j++) {
127 result[j + offset] ^= macRes[j];
128 }
129 }
130 }
131 Arrays.fill(password, (byte) 0);
132 return result;
133 }
134
135 /**
136 * Calculate the hash code for the given data.
137 *
138 * @param data the data to hash
139 * @param nullData if the data should be filled with zeros after calculating
140 * the hash code
141 * @return the hash code
142 */
143 public static byte[] getHash(byte[] data, boolean nullData) {
144 byte[] result;
145 try {
146 result = MessageDigest.getInstance("SHA-256").digest(data);
147 } catch (NoSuchAlgorithmException e) {
148 throw new RuntimeException(e);
149 }
150 if (nullData) {
151 Arrays.fill(data, (byte) 0);
152 }
153 return result;
154 }
155
156 }