Skip to content

Commit 86e3677

Browse files
🚧 progress: Import existing code.
1 parent ad2ef01 commit 86e3677

File tree

13 files changed

+10609
-13
lines changed

13 files changed

+10609
-13
lines changed

package.json

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,25 @@
7272
"test:module": "IMPORT_MAP_PATH=test/import-maps/dist/index.module.json npm run test-cmd",
7373
"test:src": "IMPORT_MAP_PATH=test/import-maps/src/index.json npm run test-cmd"
7474
},
75-
"dependencies": {},
75+
"dependencies": {
76+
"@arithmetic-type/uint32": "^2.0.0",
77+
"@arithmetic-type/uint64": "^4.0.0",
78+
"@array-like/copy": "^0.0.1",
79+
"@failure-abstraction/error": "^6.0.1"
80+
},
7681
"devDependencies": {
77-
"@babel/core": "7.19.0",
78-
"@babel/plugin-transform-destructuring": "7.18.13",
82+
"@array-like/alloc": "0.0.1",
83+
"@babel/core": "7.19.3",
84+
"@babel/plugin-transform-destructuring": "7.19.4",
7985
"@babel/plugin-transform-for-of": "7.18.8",
80-
"@babel/preset-env": "7.19.0",
86+
"@babel/preset-env": "7.19.4",
87+
"@codec-bytes/ascii": "3.0.0",
8188
"@commitlint/cli": "17.1.2",
8289
"@js-library/commitlint-config": "0.0.4",
8390
"@node-loader/babel": "2.0.1",
8491
"@node-loader/core": "2.0.0",
8592
"@node-loader/import-maps": "1.1.0",
93+
"@set-theory/cartesian-product": "2.0.2",
8694
"ava": "4.3.3",
8795
"babel-plugin-transform-remove-console": "6.9.4",
8896
"babel-plugin-unassert": "3.2.0",
@@ -100,8 +108,8 @@
100108
"np": "7.6.2",
101109
"pinst": "3.0.0",
102110
"power-assert": "1.6.1",
103-
"regenerator-runtime": "0.13.9",
104-
"xo": "0.52.3"
111+
"regenerator-runtime": "0.13.10",
112+
"xo": "0.52.4"
105113
},
106114
"ava": {
107115
"files": [
@@ -229,6 +237,16 @@
229237
"doc/**"
230238
],
231239
"env": "browser"
240+
},
241+
{
242+
"files": [
243+
"src/**"
244+
],
245+
"rules": {
246+
"unicorn/filename-case": "off",
247+
"no-bitwise": "off",
248+
"unicorn/prefer-math-trunc": "off"
249+
}
232250
}
233251
]
234252
}

src/SHA512Hasher.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {copy} from '@array-like/copy';
2+
import {NotImplementedError} from '@failure-abstraction/error';
3+
4+
import {sha512InitialState, sha512Call, sha512Finalize} from './sha512.js';
5+
6+
export class SHA512Hasher {
7+
constructor() {
8+
this._state = sha512InitialState();
9+
this._totalBits = 0;
10+
this._buffer = new Uint8Array(128);
11+
this._bufferBits = 0;
12+
}
13+
14+
update(bytes, offset = 0, length = bytes.length - offset) {
15+
if ((this._bufferBits & 0xf_ff) !== 0) {
16+
throw new NotImplementedError('Cannot add bytes to bits (for now).');
17+
}
18+
19+
this._totalBits += length << 3;
20+
21+
const bufferBytes = this._bufferBits >> 3;
22+
23+
if (bufferBytes > 0) {
24+
const bufferComplement = 128 - bufferBytes;
25+
26+
if (length < bufferComplement) {
27+
// Buffer is not full
28+
copy(bytes, offset, offset + length, this._buffer, bufferBytes);
29+
this._bufferBits += length << 3;
30+
return this;
31+
}
32+
33+
// Pad buffer with head of bytes and process it
34+
copy(bytes, offset, offset + bufferComplement, this._buffer, bufferBytes);
35+
sha512Call(this._state, this._buffer, 0);
36+
this._bufferBits = 0;
37+
offset += bufferComplement;
38+
length -= bufferComplement;
39+
}
40+
41+
const fullBlocks = length >> 7;
42+
43+
for (let i = 0; i < fullBlocks; ++i) {
44+
sha512Call(this._state, bytes, offset);
45+
offset += 128;
46+
length -= 128;
47+
}
48+
49+
copy(bytes, offset, offset + length, this._buffer, 0);
50+
this._bufferBits = length << 3;
51+
return this;
52+
}
53+
54+
digest(digest) {
55+
return sha512Finalize(
56+
this._buffer,
57+
this._totalBits,
58+
digest,
59+
this._bufferBits >> 3,
60+
0,
61+
this._bufferBits,
62+
this._state,
63+
);
64+
}
65+
}

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
const answer = 42;
2-
export default answer;
1+
export * from './SHA512Hasher.js';
2+
export * from './sha224.js';
3+
export * from './sha256.js';
4+
export * from './sha384.js';
5+
export * from './sha512.js';

0 commit comments

Comments
 (0)