Hasher.js
2.0 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
import { BufferedBlockAlgorithm } from "./BufferedBlockAlgorithm";
export class Hasher extends BufferedBlockAlgorithm {
constructor(props) {
super(props);
this._blockSize = 512 / 32;
this._props = props;
if (props && typeof props.blockSize === "number") {
this._blockSize = props.blockSize;
}
this.reset(props ? props.data : undefined, props ? props.nBytes : undefined);
}
get blockSize() {
return this._blockSize;
}
/**
* Resets this hasher to its initial state.
*
* @example
* hasher.reset();
*/
reset(data, nBytes) {
// Reset data buffer
super.reset.call(this, data, nBytes);
// Perform concrete-hasher logic
this._doReset();
}
/**
* Updates this hasher with a message.
*
* @param {Word32Array|string} messageUpdate The message to append.
* @return {Hasher} This hasher.
* @example
* hasher.update('message');
* hasher.update(wordArray);
*/
update(messageUpdate) {
this._append(messageUpdate);
this._process();
return this;
}
/**
* Finalizes the hash computation.
* Note that the finalize operation is effectively a destructive, read-once operation.
*
* @param {Word32Array|string?} messageUpdate (Optional) A final message update.
* @return {Word32Array} The hash.
* @example
* var hash = hasher.finalize();
* var hash = hasher.finalize('message');
* var hash = hasher.finalize(wordArray);
*/
finalize(messageUpdate) {
// Final message update
if (messageUpdate) {
this._append(messageUpdate);
}
// Perform concrete-hasher logic
return this._doFinalize();
}
/**
* @abstract
*/
_doReset() {
throw new Error("Not implemented");
}
/**
* @abstract
*/
_doFinalize() {
throw new Error("Not implemented");
}
}