-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSHA2.java
More file actions
213 lines (187 loc) · 7.27 KB
/
Copy pathSHA2.java
File metadata and controls
213 lines (187 loc) · 7.27 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright 2016-2022 chronicle.software
*
* https://chronicle.software
*
* 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
*
* https://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 net.openhft.chronicle.salt;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.core.Maths;
import static net.openhft.chronicle.salt.Sodium.*;
public enum SHA2 {
; // none
static final int HASH_SHA256_BYTES = 32;
static final int HASH_SHA512_BYTES = 64;
/**
* Generate sha256 hash for a given message
*
* @param message
* - the message to hash
* @return - the sha256 hash
*/
public static BytesStore sha256(BytesStore message) {
return sha256(null, message);
}
/**
* Generate hash for a given message
*
* @param result
* - the BytesStore to hold the result
* @param message
* - the message to hash
* @return - the sha256 hash
*/
public static BytesStore sha256(BytesStore result, BytesStore message) {
result = Sodium.Util.setSize(result, HASH_SHA256_BYTES);
checkValid(Sodium.SODIUM.crypto_hash_sha256(result.addressForWrite(0), message.addressForRead(message.readPosition()),
Maths.toUInt31(message.readRemaining())), "couldn't SHA256");
return result;
}
/**
* Append the sha256 hash of a message to a given Bytes handle
*
* @param hash256
* - the Bytes handle onto which the sha256 hash is appended
* @param message
* - the message to hash
*/
public static void appendSha256(Bytes<?> hash256, BytesStore<?, ?> message) {
long wp = hash256.writePosition();
hash256.ensureCapacity(wp + HASH_SHA256_BYTES);
checkValid(Sodium.SODIUM.crypto_hash_sha256(hash256.addressForWrite(wp), message.addressForRead(message.readPosition()),
Maths.toUInt31(message.readRemaining())), "couldn't SHA256");
hash256.writeSkip(HASH_SHA256_BYTES);
}
/**
* Generate sha512 hash for a given message
*
* @param message
* - the message to hash
* @return - the sha256 hash
*/
public static BytesStore sha512(BytesStore message) {
return sha512(null, message);
}
/**
* Generate sha512 hash for a given message
*
* @param result
* - the BytesStore to hold the result
* @param message
* - the message to hash
* @return - the sha512 hash
*/
public static BytesStore sha512(BytesStore result, BytesStore message) {
result = Sodium.Util.setSize(result, HASH_SHA512_BYTES);
checkValid(Sodium.SODIUM.crypto_hash_sha512(result.addressForWrite(0), message.addressForRead(message.readPosition()),
Maths.toUInt31(message.readRemaining())), "Couldn't SHA512");
return result;
}
/**
* Append the sha512 hash of a message to a given Bytes handle
*
* @param hash512
* - the Bytes handle onto which the sha512 hash is appended
* @param message
* - the message to hash
*/
public static void appendSha512(Bytes<?> hash512, BytesStore<?, ?> message) {
long wp = hash512.writePosition();
hash512.ensureCapacity(wp + HASH_SHA512_BYTES);
checkValid(Sodium.SODIUM.crypto_hash_sha512(hash512.addressForWrite(wp), message.addressForRead(message.readPosition()),
Maths.toUInt31(message.readRemaining())), "Couldn't SHA512");
hash512.writeSkip(HASH_SHA512_BYTES);
}
/**
* Wrapper for SHA-256 signing multi-part messages composed of a sequence of arbitrarily-sized chunks
*/
public static class MultiPartSHA256 {
public final BytesStore state;
/**
* Initialise a wrapper for a single multi-part message exchange
*/
public MultiPartSHA256() {
this.state = Bytes.allocateDirect(SIZEOF_CRYPTO_HASH_SHA256_STATE);
((Bytes) state).readLimit(SIZEOF_CRYPTO_HASH_SHA256_STATE);
Sodium.SODIUM.crypto_hash_sha256_init(state.addressForRead(0));
}
public void reset() {
Sodium.SODIUM.crypto_hash_sha256_init(state.addressForRead(0));
}
/**
* Add a part to this multi-part hash
*
* @param message
* - the message to add
*/
public void add(BytesStore message) {
checkValid(Sodium.SODIUM.crypto_hash_sha256_update(state.addressForRead(0), message.addressForRead(message.readPosition()),
message.readRemaining()), "Failed to add to multi-part message");
}
/**
* Generate the single hash for the collection of messages
*
* @return - the single hash
*/
public BytesStore hash() {
return hash(null);
}
public BytesStore hash(BytesStore result) {
result = Sodium.Util.setSize(result, HASH_SHA256_BYTES);
checkValid(Sodium.SODIUM.crypto_hash_sha256_final(state.addressForRead(0), result.addressForWrite(0)), "Multi-part SHA256 failed");
return result;
}
}
/**
* Wrapper for SHA-512 signing multi-part messages composed of a sequence of arbitrarily-sized chunks
*/
public static class MultiPartSHA512 {
public final BytesStore state;
/**
* Initialise a wrapper for a single multi-part message exchange
*/
public MultiPartSHA512() {
this.state = Bytes.allocateDirect(SIZEOF_CRYPTO_HASH_SHA512_STATE);
((Bytes) state).readLimit(SIZEOF_CRYPTO_HASH_SHA512_STATE);
Sodium.SODIUM.crypto_hash_sha512_init(state.addressForRead(0));
}
public void reset() {
Sodium.SODIUM.crypto_hash_sha512_init(state.addressForRead(0));
}
/**
* Add a part to this multi-part hash
*
* @param message
* - the message to add
*/
public void add(BytesStore message) {
checkValid(Sodium.SODIUM.crypto_hash_sha512_update(state.addressForRead(0), message.addressForRead(message.readPosition()),
message.readRemaining()), "Failed to add to multi-part message");
}
/**
* Generate the single hash for the collection of messages
*
* @return - the single hash
*/
public BytesStore hash() {
return hash(null);
}
public BytesStore hash(BytesStore result) {
result = Sodium.Util.setSize(result, HASH_SHA512_BYTES);
checkValid(Sodium.SODIUM.crypto_hash_sha512_final(state.addressForRead(0), result.addressForWrite(0)), "Multi-part SHA512 failed");
return result;
}
}
}