@kokuin/crypto 0.7.0 → 0.7.1
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/lib/bytes.d.ts +2 -0
- package/lib/bytes.js +7 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/noble.js +5 -10
- package/package.json +1 -1
package/lib/bytes.d.ts
ADDED
package/lib/bytes.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Accept byte views across realms while excluding shared and detached buffers. */ export function assertUnsharedBytes(value) {
|
|
2
|
+
if (!ArrayBuffer.isView(value) || Object.prototype.toString.call(value) !== '[object Uint8Array]' || Object.prototype.toString.call(value.buffer) !== '[object ArrayBuffer]') {
|
|
3
|
+
throw new TypeError('Expected an unshared Uint8Array');
|
|
4
|
+
}
|
|
5
|
+
// Throws TypeError on a detached buffer.
|
|
6
|
+
new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
7
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
+
export { assertUnsharedBytes } from './bytes.js';
|
|
1
2
|
export { nobleCrypto } from './noble.js';
|
|
2
3
|
export type { CryptoAlgorithm, CryptoProvider, CryptoProviderBlake3BatchParams, CryptoProviderBlake3Params, CryptoProviderSignBatchParams, CryptoProviderSignParams, SignItem, VerifyItem, } from './types.js';
|
package/lib/index.js
CHANGED
package/lib/noble.js
CHANGED
|
@@ -2,16 +2,11 @@ import { ed25519 } from '@noble/curves/ed25519.js';
|
|
|
2
2
|
import { p256 } from '@noble/curves/nist.js';
|
|
3
3
|
import { blake3 } from '@noble/hashes/blake3.js';
|
|
4
4
|
import { bytesToHex } from '@noble/hashes/utils.js';
|
|
5
|
-
|
|
6
|
-
if (!(value instanceof Uint8Array) || !(value.buffer instanceof ArrayBuffer)) {
|
|
7
|
-
throw new TypeError('Expected an unshared Uint8Array');
|
|
8
|
-
}
|
|
9
|
-
new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
10
|
-
}
|
|
5
|
+
import { assertUnsharedBytes } from './bytes.js';
|
|
11
6
|
function sign(alg, seed, message) {
|
|
12
7
|
if (alg !== 'EdDSA') throw new TypeError('Unsupported signing algorithm');
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
assertUnsharedBytes(seed);
|
|
9
|
+
assertUnsharedBytes(message);
|
|
15
10
|
if (seed.length !== 32) throw new TypeError('Expected a 32-byte Ed25519 seed');
|
|
16
11
|
return ed25519.sign(message, seed);
|
|
17
12
|
}
|
|
@@ -44,7 +39,7 @@ function verify({ alg, signature, message, publicKey }) {
|
|
|
44
39
|
signature,
|
|
45
40
|
message,
|
|
46
41
|
publicKey
|
|
47
|
-
])
|
|
42
|
+
])assertUnsharedBytes(bytes);
|
|
48
43
|
if (signature.length !== 64) return false;
|
|
49
44
|
try {
|
|
50
45
|
if (alg === 'EdDSA') {
|
|
@@ -66,7 +61,7 @@ function assertOutputLength(dkLen) {
|
|
|
66
61
|
if (!Number.isInteger(dkLen) || dkLen < 1 || dkLen > 65536) throw new RangeError('Invalid blake3 output length');
|
|
67
62
|
}
|
|
68
63
|
function hash(input, dkLen) {
|
|
69
|
-
|
|
64
|
+
assertUnsharedBytes(input);
|
|
70
65
|
assertOutputLength(dkLen);
|
|
71
66
|
return blake3(input, {
|
|
72
67
|
dkLen
|