1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.common.hash;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.primitives.Chars;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* {@link HashFunction} adapter for {@link MessageDigest}s.
*
* @author Kevin Bourrillion
* @author Dimitris Andreou
*/
final class MessageDigestHashFunction extends AbstractStreamingHashFunction {
private final String algorithmName;
private final int bits;
MessageDigestHashFunction(String algorithmName) {
this.algorithmName = algorithmName;
this.bits = getMessageDigest(algorithmName).getDigestLength() * 8;
}
public int bits() {
return bits;
}
private static MessageDigest getMessageDigest(String algorithmName) {
try {
return MessageDigest.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
@Override public Hasher newHasher() {
return new MessageDigestHasher(getMessageDigest(algorithmName));
}
private static class MessageDigestHasher implements Hasher {
private final MessageDigest digest;
private final ByteBuffer scratch; // lazy convenience
private boolean done;
private MessageDigestHasher(MessageDigest digest) {
this.digest = digest;
this.scratch = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
}
@Override public Hasher putByte(byte b) {
checkNotDone();
digest.update(b);
return this;
}
@Override public Hasher putBytes(byte[] bytes) {
checkNotDone();
digest.update(bytes);
return this;
}
@Override public Hasher putBytes(byte[] bytes, int off, int len) {
checkNotDone();
checkPositionIndexes(off, off + len, bytes.length);
digest.update(bytes, off, len);
return this;
}
@Override public Hasher putShort(short s) {
checkNotDone();
scratch.putShort(s);
digest.update(scratch.array(), 0, Shorts.BYTES);
scratch.clear();
return this;
}
@Override public Hasher putInt(int i) {
checkNotDone();
scratch.putInt(i);
digest.update(scratch.array(), 0, Ints.BYTES);
scratch.clear();
return this;
}
@Override public Hasher putLong(long l) {
checkNotDone();
scratch.putLong(l);
digest.update(scratch.array(), 0, Longs.BYTES);
scratch.clear();
return this;
}
@Override public Hasher putFloat(float f) {
checkNotDone();
scratch.putFloat(f);
digest.update(scratch.array(), 0, 4);
scratch.clear();
return this;
}
@Override public Hasher putDouble(double d) {
checkNotDone();
scratch.putDouble(d);
digest.update(scratch.array(), 0, 8);
scratch.clear();
return this;
}
@Override public Hasher putBoolean(boolean b) {
return putByte(b ? (byte) 1 : (byte) 0);
}
@Override public Hasher putChar(char c) {
checkNotDone();
scratch.putChar(c);
digest.update(scratch.array(), 0, Chars.BYTES);
scratch.clear();
return this;
}
@Override public Hasher putString(CharSequence charSequence) {
for (int i = 0; i < charSequence.length(); i++) {
putChar(charSequence.charAt(i));
}
return this;
}
@Override public Hasher putString(CharSequence charSequence, Charset charset) {
return putBytes(charSequence.toString().getBytes(charset));
}
@Override public <T> Hasher putObject(T instance, Funnel<? super T> funnel) {
checkNotDone();
funnel.funnel(instance, this);
return this;
}
private void checkNotDone() {
checkState(!done, "Cannot use Hasher after calling #hash() on it");
}
public HashCode hash() {
done = true;
return HashCodes.fromBytesNoCopy(digest.digest());
}
}
}
|