@libp2p/crypto 5.1.4 → 5.1.5
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/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/keys/ecdsa/ecdsa.d.ts +3 -3
- package/dist/src/keys/ecdsa/ecdsa.d.ts.map +1 -1
- package/dist/src/keys/ecdsa/ecdsa.js +4 -4
- package/dist/src/keys/ecdsa/ecdsa.js.map +1 -1
- package/dist/src/keys/ecdsa/index.d.ts +3 -2
- package/dist/src/keys/ecdsa/index.d.ts.map +1 -1
- package/dist/src/keys/ecdsa/index.js +8 -3
- package/dist/src/keys/ecdsa/index.js.map +1 -1
- package/dist/src/keys/ed25519/ed25519.d.ts +3 -3
- package/dist/src/keys/ed25519/ed25519.d.ts.map +1 -1
- package/dist/src/keys/ed25519/ed25519.js +22 -4
- package/dist/src/keys/ed25519/ed25519.js.map +1 -1
- package/dist/src/keys/ed25519/index.browser.d.ts +2 -5
- package/dist/src/keys/ed25519/index.browser.d.ts.map +1 -1
- package/dist/src/keys/ed25519/index.browser.js +61 -5
- package/dist/src/keys/ed25519/index.browser.js.map +1 -1
- package/dist/src/keys/ed25519/index.d.ts +1 -1
- package/dist/src/keys/ed25519/index.d.ts.map +1 -1
- package/dist/src/keys/rsa/index.browser.d.ts +4 -3
- package/dist/src/keys/rsa/index.browser.d.ts.map +1 -1
- package/dist/src/keys/rsa/index.browser.js +15 -7
- package/dist/src/keys/rsa/index.browser.js.map +1 -1
- package/dist/src/keys/rsa/index.d.ts +4 -3
- package/dist/src/keys/rsa/index.d.ts.map +1 -1
- package/dist/src/keys/rsa/index.js +8 -5
- package/dist/src/keys/rsa/index.js.map +1 -1
- package/dist/src/keys/rsa/rsa.d.ts +3 -3
- package/dist/src/keys/rsa/rsa.d.ts.map +1 -1
- package/dist/src/keys/rsa/rsa.js +4 -4
- package/dist/src/keys/rsa/rsa.js.map +1 -1
- package/dist/src/keys/secp256k1/index.browser.d.ts +3 -2
- package/dist/src/keys/secp256k1/index.browser.d.ts.map +1 -1
- package/dist/src/keys/secp256k1/index.browser.js +19 -4
- package/dist/src/keys/secp256k1/index.browser.js.map +1 -1
- package/dist/src/keys/secp256k1/index.d.ts +3 -2
- package/dist/src/keys/secp256k1/index.d.ts.map +1 -1
- package/dist/src/keys/secp256k1/index.js +4 -2
- package/dist/src/keys/secp256k1/index.js.map +1 -1
- package/dist/src/keys/secp256k1/secp256k1.d.ts +3 -3
- package/dist/src/keys/secp256k1/secp256k1.d.ts.map +1 -1
- package/dist/src/keys/secp256k1/secp256k1.js +4 -4
- package/dist/src/keys/secp256k1/secp256k1.js.map +1 -1
- package/package.json +5 -5
- package/src/keys/ecdsa/ecdsa.ts +5 -5
- package/src/keys/ecdsa/index.ts +10 -3
- package/src/keys/ed25519/ed25519.ts +27 -5
- package/src/keys/ed25519/index.browser.ts +72 -5
- package/src/keys/ed25519/index.ts +1 -1
- package/src/keys/rsa/index.browser.ts +18 -7
- package/src/keys/rsa/index.ts +11 -5
- package/src/keys/rsa/rsa.ts +5 -5
- package/src/keys/secp256k1/index.browser.ts +22 -4
- package/src/keys/secp256k1/index.ts +6 -2
- package/src/keys/secp256k1/secp256k1.ts +5 -5
package/src/keys/rsa/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import crypto from 'crypto'
|
|
2
|
-
import { promisify } from 'util'
|
|
1
|
+
import crypto from 'node:crypto'
|
|
2
|
+
import { promisify } from 'node:util'
|
|
3
3
|
import { InvalidParametersError } from '@libp2p/interface'
|
|
4
4
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
5
|
import randomBytes from '../../random-bytes.js'
|
|
6
6
|
import * as utils from './utils.js'
|
|
7
7
|
import type { JWKKeyPair } from '../interface.js'
|
|
8
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
8
9
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
const keypair = promisify(crypto.generateKeyPair)
|
|
@@ -13,13 +14,14 @@ export const RSAES_PKCS1_V1_5_OID = '1.2.840.113549.1.1.1'
|
|
|
13
14
|
|
|
14
15
|
export { utils }
|
|
15
16
|
|
|
16
|
-
export async function generateRSAKey (bits: number): Promise<JWKKeyPair> {
|
|
17
|
+
export async function generateRSAKey (bits: number, options?: AbortOptions): Promise<JWKKeyPair> {
|
|
17
18
|
// @ts-expect-error node types are missing jwk as a format
|
|
18
19
|
const key = await keypair('rsa', {
|
|
19
20
|
modulusLength: bits,
|
|
20
21
|
publicKeyEncoding: { type: 'pkcs1', format: 'jwk' },
|
|
21
22
|
privateKeyEncoding: { type: 'pkcs1', format: 'jwk' }
|
|
22
23
|
})
|
|
24
|
+
options?.signal?.throwIfAborted()
|
|
23
25
|
|
|
24
26
|
return {
|
|
25
27
|
// @ts-expect-error node types are missing jwk as a format
|
|
@@ -31,7 +33,9 @@ export async function generateRSAKey (bits: number): Promise<JWKKeyPair> {
|
|
|
31
33
|
|
|
32
34
|
export { randomBytes as getRandomValues }
|
|
33
35
|
|
|
34
|
-
export
|
|
36
|
+
export function hashAndSign (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array {
|
|
37
|
+
options?.signal?.throwIfAborted()
|
|
38
|
+
|
|
35
39
|
const hash = crypto.createSign('RSA-SHA256')
|
|
36
40
|
|
|
37
41
|
if (msg instanceof Uint8Array) {
|
|
@@ -46,7 +50,9 @@ export async function hashAndSign (key: JsonWebKey, msg: Uint8Array | Uint8Array
|
|
|
46
50
|
return hash.sign({ format: 'jwk', key })
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
export
|
|
53
|
+
export function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList, options?: AbortOptions): boolean {
|
|
54
|
+
options?.signal?.throwIfAborted()
|
|
55
|
+
|
|
50
56
|
const hash = crypto.createVerify('RSA-SHA256')
|
|
51
57
|
|
|
52
58
|
if (msg instanceof Uint8Array) {
|
package/src/keys/rsa/rsa.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { base58btc } from 'multiformats/bases/base58'
|
|
|
2
2
|
import { CID } from 'multiformats/cid'
|
|
3
3
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
4
4
|
import { hashAndSign, utils, hashAndVerify } from './index.js'
|
|
5
|
-
import type { RSAPublicKey as RSAPublicKeyInterface, RSAPrivateKey as RSAPrivateKeyInterface } from '@libp2p/interface'
|
|
5
|
+
import type { RSAPublicKey as RSAPublicKeyInterface, RSAPrivateKey as RSAPrivateKeyInterface, AbortOptions } from '@libp2p/interface'
|
|
6
6
|
import type { Digest } from 'multiformats/hashes/digest'
|
|
7
7
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
|
|
@@ -45,8 +45,8 @@ export class RSAPublicKey implements RSAPublicKeyInterface {
|
|
|
45
45
|
return uint8ArrayEquals(this.raw, key.raw)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean> {
|
|
49
|
-
return hashAndVerify(this.jwk, sig, data)
|
|
48
|
+
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array, options?: AbortOptions): boolean | Promise<boolean> {
|
|
49
|
+
return hashAndVerify(this.jwk, sig, data, options)
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
@@ -77,7 +77,7 @@ export class RSAPrivateKey implements RSAPrivateKeyInterface {
|
|
|
77
77
|
return uint8ArrayEquals(this.raw, key.raw)
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
81
|
-
return hashAndSign(this.jwk, message)
|
|
80
|
+
sign (message: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array | Promise<Uint8Array> {
|
|
81
|
+
return hashAndSign(this.jwk, message, options)
|
|
82
82
|
}
|
|
83
83
|
}
|
|
@@ -2,6 +2,7 @@ import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
|
2
2
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
3
|
import { SigningError, VerificationError } from '../../errors.js'
|
|
4
4
|
import { isPromise } from '../../util.js'
|
|
5
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
5
6
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
7
|
|
|
7
8
|
const PUBLIC_KEY_BYTE_LENGTH = 33
|
|
@@ -13,12 +14,20 @@ export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
|
13
14
|
/**
|
|
14
15
|
* Hash and sign message with private key
|
|
15
16
|
*/
|
|
16
|
-
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
17
|
+
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array | Promise<Uint8Array> {
|
|
17
18
|
const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
18
19
|
|
|
19
20
|
if (isPromise(p)) {
|
|
20
|
-
return p
|
|
21
|
+
return p
|
|
22
|
+
.then(({ digest }) => {
|
|
23
|
+
options?.signal?.throwIfAborted()
|
|
24
|
+
return secp.sign(digest, key).toDERRawBytes()
|
|
25
|
+
})
|
|
21
26
|
.catch(err => {
|
|
27
|
+
if (err.name === 'AbortError') {
|
|
28
|
+
throw err
|
|
29
|
+
}
|
|
30
|
+
|
|
22
31
|
throw new SigningError(String(err))
|
|
23
32
|
})
|
|
24
33
|
}
|
|
@@ -33,17 +42,26 @@ export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList):
|
|
|
33
42
|
/**
|
|
34
43
|
* Hash message and verify signature with public key
|
|
35
44
|
*/
|
|
36
|
-
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean | Promise<boolean> {
|
|
45
|
+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList, options?: AbortOptions): boolean | Promise<boolean> {
|
|
37
46
|
const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
38
47
|
|
|
39
48
|
if (isPromise(p)) {
|
|
40
|
-
return p
|
|
49
|
+
return p
|
|
50
|
+
.then(({ digest }) => {
|
|
51
|
+
options?.signal?.throwIfAborted()
|
|
52
|
+
return secp.verify(sig, digest, key)
|
|
53
|
+
})
|
|
41
54
|
.catch(err => {
|
|
55
|
+
if (err.name === 'AbortError') {
|
|
56
|
+
throw err
|
|
57
|
+
}
|
|
58
|
+
|
|
42
59
|
throw new VerificationError(String(err))
|
|
43
60
|
})
|
|
44
61
|
}
|
|
45
62
|
|
|
46
63
|
try {
|
|
64
|
+
options?.signal?.throwIfAborted()
|
|
47
65
|
return secp.verify(sig, p.digest, key)
|
|
48
66
|
} catch (err) {
|
|
49
67
|
throw new VerificationError(String(err))
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import crypto from 'node:crypto'
|
|
2
2
|
import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
3
3
|
import { SigningError, VerificationError } from '../../errors.js'
|
|
4
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
4
5
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
5
6
|
|
|
6
7
|
const PUBLIC_KEY_BYTE_LENGTH = 33
|
|
@@ -12,7 +13,9 @@ export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
|
12
13
|
/**
|
|
13
14
|
* Hash and sign message with private key
|
|
14
15
|
*/
|
|
15
|
-
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
16
|
+
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array {
|
|
17
|
+
options?.signal?.throwIfAborted()
|
|
18
|
+
|
|
16
19
|
const hash = crypto.createHash('sha256')
|
|
17
20
|
|
|
18
21
|
if (msg instanceof Uint8Array) {
|
|
@@ -36,7 +39,8 @@ export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList):
|
|
|
36
39
|
/**
|
|
37
40
|
* Hash message and verify signature with public key
|
|
38
41
|
*/
|
|
39
|
-
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
|
|
42
|
+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList, options?: AbortOptions): boolean {
|
|
43
|
+
options?.signal?.throwIfAborted()
|
|
40
44
|
const hash = crypto.createHash('sha256')
|
|
41
45
|
|
|
42
46
|
if (msg instanceof Uint8Array) {
|
|
@@ -5,7 +5,7 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
|
5
5
|
import { publicKeyToProtobuf } from '../index.js'
|
|
6
6
|
import { validateSecp256k1PublicKey, compressSecp256k1PublicKey, computeSecp256k1PublicKey, validateSecp256k1PrivateKey } from './utils.js'
|
|
7
7
|
import { hashAndVerify, hashAndSign } from './index.js'
|
|
8
|
-
import type { Secp256k1PublicKey as Secp256k1PublicKeyInterface, Secp256k1PrivateKey as Secp256k1PrivateKeyInterface } from '@libp2p/interface'
|
|
8
|
+
import type { Secp256k1PublicKey as Secp256k1PublicKeyInterface, Secp256k1PrivateKey as Secp256k1PrivateKeyInterface, AbortOptions } from '@libp2p/interface'
|
|
9
9
|
import type { Digest } from 'multiformats/hashes/digest'
|
|
10
10
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
11
11
|
|
|
@@ -39,8 +39,8 @@ export class Secp256k1PublicKey implements Secp256k1PublicKeyInterface {
|
|
|
39
39
|
return uint8ArrayEquals(this.raw, key.raw)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean {
|
|
43
|
-
return hashAndVerify(this._key, sig, data)
|
|
42
|
+
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array, options?: AbortOptions): boolean {
|
|
43
|
+
return hashAndVerify(this._key, sig, data, options)
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
@@ -62,7 +62,7 @@ export class Secp256k1PrivateKey implements Secp256k1PrivateKeyInterface {
|
|
|
62
62
|
return uint8ArrayEquals(this.raw, key.raw)
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
66
|
-
return hashAndSign(this.raw, message)
|
|
65
|
+
sign (message: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array | Promise<Uint8Array> {
|
|
66
|
+
return hashAndSign(this.raw, message, options)
|
|
67
67
|
}
|
|
68
68
|
}
|