@push.rocks/smartcrypto 2.2.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.smartconfig.json +1 -4
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +2 -1
- package/dist_ts/smartcrypto.classes.keypair.d.ts +6 -1
- package/dist_ts/smartcrypto.classes.keypair.js +11 -5
- package/dist_ts/smartcrypto.classes.privatekey.d.ts +13 -0
- package/dist_ts/smartcrypto.classes.privatekey.js +26 -1
- package/dist_ts/smartcrypto.classes.smartcrypto.d.ts +2 -2
- package/dist_ts/smartcrypto.classes.smartcrypto.js +3 -3
- package/dist_ts/smartcrypto.errors.d.ts +1 -1
- package/dist_ts/smartcrypto.errors.js +1 -1
- package/dist_ts/smartcrypto.hash.d.ts +15 -0
- package/dist_ts/smartcrypto.hash.js +159 -0
- package/dist_ts/smartcrypto.internal.d.ts +10 -0
- package/dist_ts/smartcrypto.internal.js +51 -1
- package/dist_ts/smartcrypto.pkcs8.d.ts +14 -0
- package/dist_ts/smartcrypto.pkcs8.js +252 -0
- package/dist_ts_node/index.d.ts +1 -0
- package/dist_ts_node/index.js +2 -0
- package/dist_ts_node/smartcrypto.hash.d.ts +17 -0
- package/dist_ts_node/smartcrypto.hash.js +60 -0
- package/dist_ts_node/smartcrypto.plugins.d.ts +4 -0
- package/dist_ts_node/smartcrypto.plugins.js +6 -0
- package/package.json +16 -8
- package/readme.md +110 -3
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/index.ts +1 -0
- package/ts/smartcrypto.classes.keypair.ts +18 -4
- package/ts/smartcrypto.classes.privatekey.ts +34 -0
- package/ts/smartcrypto.classes.smartcrypto.ts +3 -3
- package/ts/smartcrypto.errors.ts +1 -0
- package/ts/smartcrypto.hash.ts +203 -0
- package/ts/smartcrypto.internal.ts +57 -0
- package/ts/smartcrypto.pkcs8.ts +311 -0
- package/ts_node/index.ts +1 -0
- package/ts_node/smartcrypto.hash.ts +88 -0
- package/ts_node/smartcrypto.plugins.ts +6 -0
- package/ts_node/tsconfig.json +6 -0
- package/readme.hints.md +0 -1
- /package/{license → license.md} +0 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as plugins from './smartcrypto.plugins.js';
|
|
2
|
+
import { createSmartCryptoError } from '../ts/smartcrypto.errors.js';
|
|
3
|
+
import type { ISha256Hasher } from '../ts/smartcrypto.hash.js';
|
|
4
|
+
import {
|
|
5
|
+
hashReadableStream,
|
|
6
|
+
requireHashInput,
|
|
7
|
+
requireString,
|
|
8
|
+
} from '../ts/smartcrypto.internal.js';
|
|
9
|
+
|
|
10
|
+
export type { ISha256Hasher };
|
|
11
|
+
|
|
12
|
+
const textEncoder = new TextEncoder();
|
|
13
|
+
|
|
14
|
+
/** SHA-256 backed by `node:crypto`. */
|
|
15
|
+
class NodeSha256Hasher implements ISha256Hasher {
|
|
16
|
+
private readonly hash = plugins.crypto.createHash('sha256');
|
|
17
|
+
private finalized = false;
|
|
18
|
+
|
|
19
|
+
public update(dataArg: ArrayBuffer | Uint8Array): this {
|
|
20
|
+
this.requireOpen();
|
|
21
|
+
this.hash.update(requireHashInput(dataArg));
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public digest(): string {
|
|
26
|
+
this.requireOpen();
|
|
27
|
+
this.finalized = true;
|
|
28
|
+
return this.hash.digest('hex');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private requireOpen(): void {
|
|
32
|
+
if (this.finalized) {
|
|
33
|
+
throw createSmartCryptoError('INVALID_STATE');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Node stream chunks are bytes, or strings hashed as UTF-8. */
|
|
39
|
+
const requireStreamChunk = (chunkArg: unknown): Uint8Array => {
|
|
40
|
+
if (typeof chunkArg === 'string') {
|
|
41
|
+
return textEncoder.encode(chunkArg);
|
|
42
|
+
}
|
|
43
|
+
return requireHashInput(chunkArg);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const hashNodeReadable = async (
|
|
47
|
+
streamArg: plugins.stream.Readable,
|
|
48
|
+
hasherArg: ISha256Hasher,
|
|
49
|
+
): Promise<string> => {
|
|
50
|
+
// Leaving the loop early (failure or invalid chunk) destroys the stream.
|
|
51
|
+
for await (const chunk of streamArg) {
|
|
52
|
+
hasherArg.update(requireStreamChunk(chunk));
|
|
53
|
+
}
|
|
54
|
+
return hasherArg.digest();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** Creates an incremental SHA-256 hasher backed by `node:crypto`. */
|
|
58
|
+
export const createSha256Hasher = (): ISha256Hasher => new NodeSha256Hasher();
|
|
59
|
+
|
|
60
|
+
/** SHA-256 of the UTF-8 bytes of a string, computed synchronously. */
|
|
61
|
+
export const sha256FromStringSync = (stringArg: string): string =>
|
|
62
|
+
plugins.crypto.createHash('sha256').update(requireString(stringArg), 'utf8').digest('hex');
|
|
63
|
+
|
|
64
|
+
/** SHA-256 of the UTF-8 bytes of a string. */
|
|
65
|
+
export const sha256FromString = async (stringArg: string): Promise<string> =>
|
|
66
|
+
sha256FromStringSync(stringArg);
|
|
67
|
+
|
|
68
|
+
/** SHA-256 of bytes. */
|
|
69
|
+
export const sha256FromBuffer = async (bufferArg: ArrayBuffer | Uint8Array): Promise<string> =>
|
|
70
|
+
createSha256Hasher().update(bufferArg).digest();
|
|
71
|
+
|
|
72
|
+
/** SHA-256 of a Node.js readable or a WHATWG byte stream, hashed incrementally. */
|
|
73
|
+
export const sha256FromStream = async (
|
|
74
|
+
streamArg: plugins.stream.Readable | ReadableStream<Uint8Array>,
|
|
75
|
+
): Promise<string> => {
|
|
76
|
+
if (streamArg instanceof plugins.stream.Readable) {
|
|
77
|
+
return await hashNodeReadable(streamArg, createSha256Hasher());
|
|
78
|
+
}
|
|
79
|
+
return await hashReadableStream(streamArg, createSha256Hasher());
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** SHA-256 of a file's contents, streamed from disk. */
|
|
83
|
+
export const sha256FromFile = async (filePathArg: string): Promise<string> =>
|
|
84
|
+
await sha256FromStream(plugins.fs.createReadStream(requireString(filePathArg)));
|
|
85
|
+
|
|
86
|
+
/** MD5 of the UTF-8 bytes of a string. MD5 is broken for security purposes; use it only for legacy checksums. */
|
|
87
|
+
export const md5FromString = async (stringArg: string): Promise<string> =>
|
|
88
|
+
plugins.crypto.createHash('md5').update(requireString(stringArg), 'utf8').digest('hex');
|
package/readme.hints.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
/package/{license → license.md}
RENAMED
|
File without changes
|