Debian Codegraph @

src/main/org/h2/expression/function/HashFunction.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.expression.function;
7
8 import java.io.InputStream;
9 import java.nio.charset.StandardCharsets;
10 import java.security.MessageDigest;
11
12 import org.h2.engine.SessionLocal;
13 import org.h2.expression.Expression;
14 import org.h2.expression.TypedValueExpression;
15 import org.h2.message.DbException;
16 import org.h2.security.SHA3;
17 import org.h2.util.Bits;
18 import org.h2.util.StringUtils;
19 import org.h2.value.TypeInfo;
20 import org.h2.value.Value;
21 import org.h2.value.ValueBigint;
22 import org.h2.value.ValueNull;
23 import org.h2.value.ValueVarbinary;
24
25 /**
26 * A HASH or ORA_HASH function.
27 */
28 public final class HashFunction extends FunctionN {
29
30 /**
31 * HASH() (non-standard).
32 */
33 public static final int HASH = 0;
34
35 /**
36 * ORA_HASH() (non-standard).
37 */
38 public static final int ORA_HASH = HASH + 1;
39
40 private static final String[] NAMES = { //
41 "HASH", "ORA_HASH" //
42 };
43
44 private final int function;
45
46 public HashFunction(Expression arg, int function) {
47 super(new Expression[] { arg });
48 this.function = function;
49 }
50
51 public HashFunction(Expression arg1, Expression arg2, Expression arg3, int function) {
52 super(arg3 == null ? new Expression[] { arg1, arg2 } : new Expression[] { arg1, arg2, arg3 });
53 this.function = function;
54 }
55
56 @Override
57 public Value getValue(SessionLocal session, Value v1, Value v2, Value v3) {
58 switch (function) {
59 case HASH:
60 v1 = getHash(v1.getString(), v2, v3 == null ? 1 : v3.getInt());
61 break;
62 case ORA_HASH:
63 v1 = oraHash(v1, v2 == null ? 0xffff_ffffL : v2.getLong(), v3 == null ? 0L : v3.getLong());
64 break;
65 default:
66 throw DbException.getInternalError("function=" + function);
67 }
68 return v1;
69 }
70
71 private static Value getHash(String algorithm, Value value, int iterations) {
72 if (iterations <= 0) {
73 throw DbException.getInvalidValueException("iterations", iterations);
74 }
75 MessageDigest md;
76 switch (StringUtils.toUpperEnglish(algorithm)) {
77 case "MD5":
78 case "SHA-1":
79 case "SHA-224":
80 case "SHA-256":
81 case "SHA-384":
82 case "SHA-512":
83 md = hashImpl(value, algorithm);
84 break;
85 case "SHA256":
86 md = hashImpl(value, "SHA-256");
87 break;
88 case "SHA3-224":
89 md = hashImpl(value, SHA3.getSha3_224());
90 break;
91 case "SHA3-256":
92 md = hashImpl(value, SHA3.getSha3_256());
93 break;
94 case "SHA3-384":
95 md = hashImpl(value, SHA3.getSha3_384());
96 break;
97 case "SHA3-512":
98 md = hashImpl(value, SHA3.getSha3_512());
99 break;
100 default:
101 throw DbException.getInvalidValueException("algorithm", algorithm);
102 }
103 byte[] b = md.digest();
104 for (int i = 1; i < iterations; i++) {
105 b = md.digest(b);
106 }
107 return ValueVarbinary.getNoCopy(b);
108 }
109
110 private static Value oraHash(Value value, long bucket, long seed) {
111 if ((bucket & 0xffff_ffff_0000_0000L) != 0L) {
112 throw DbException.getInvalidValueException("bucket", bucket);
113 }
114 if ((seed & 0xffff_ffff_0000_0000L) != 0L) {
115 throw DbException.getInvalidValueException("seed", seed);
116 }
117 MessageDigest md = hashImpl(value, "SHA-1");
118 if (md == null) {
119 return ValueNull.INSTANCE;
120 }
121 if (seed != 0L) {
122 byte[] b = new byte[4];
123 Bits.writeInt(b, 0, (int) seed);
124 md.update(b);
125 }
126 long hc = Bits.readLong(md.digest(), 0);
127 // Strip sign and use modulo operation to get value from 0 to bucket
128 // inclusive
129 return ValueBigint.get((hc & Long.MAX_VALUE) % (bucket + 1));
130 }
131
132 private static MessageDigest hashImpl(Value value, String algorithm) {
133 MessageDigest md;
134 try {
135 md = MessageDigest.getInstance(algorithm);
136 } catch (Exception ex) {
137 throw DbException.convert(ex);
138 }
139 return hashImpl(value, md);
140 }
141
142 private static MessageDigest hashImpl(Value value, MessageDigest md) {
143 try {
144 switch (value.getValueType()) {
145 case Value.VARCHAR:
146 case Value.CHAR:
147 case Value.VARCHAR_IGNORECASE:
148 md.update(value.getString().getBytes(StandardCharsets.UTF_8));
149 break;
150 case Value.BLOB:
151 case Value.CLOB: {
152 byte[] buf = new byte[4096];
153 try (InputStream is = value.getInputStream()) {
154 for (int r; (r = is.read(buf)) > 0;) {
155 md.update(buf, 0, r);
156 }
157 }
158 break;
159 }
160 default:
161 md.update(value.getBytesNoCopy());
162 }
163 return md;
164 } catch (Exception ex) {
165 throw DbException.convert(ex);
166 }
167 }
168
169 @Override
170 public Expression optimize(SessionLocal session) {
171 boolean allConst = optimizeArguments(session, true);
172 switch (function) {
173 case HASH:
174 type = TypeInfo.TYPE_VARBINARY;
175 break;
176 case ORA_HASH:
177 type = TypeInfo.TYPE_BIGINT;
178 break;
179 default:
180 throw DbException.getInternalError("function=" + function);
181 }
182 if (allConst) {
183 return TypedValueExpression.getTypedIfNull(getValue(session), type);
184 }
185 return this;
186 }
187
188 @Override
189 public String getName() {
190 return NAMES[function];
191 }
192
193 }