@libp2p/crypto 2.0.8 → 3.0.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/README.md +1 -9
- package/dist/index.min.js +11 -11
- package/dist/src/aes/cipher-mode.js +1 -1
- package/dist/src/aes/cipher-mode.js.map +1 -1
- package/dist/src/aes/index.d.ts +1 -1
- package/dist/src/aes/index.d.ts.map +1 -1
- package/dist/src/aes/index.js +1 -1
- package/dist/src/aes/index.js.map +1 -1
- package/dist/src/ciphers/aes-gcm.browser.js.map +1 -1
- package/dist/src/ciphers/aes-gcm.js +3 -3
- package/dist/src/ciphers/aes-gcm.js.map +1 -1
- package/dist/src/keys/ecdh-browser.js +2 -2
- package/dist/src/keys/ecdh-browser.js.map +1 -1
- package/dist/src/keys/ecdh.js +1 -1
- package/dist/src/keys/ecdh.js.map +1 -1
- package/dist/src/keys/ed25519-browser.d.ts +5 -4
- package/dist/src/keys/ed25519-browser.d.ts.map +1 -1
- package/dist/src/keys/ed25519-browser.js +6 -6
- package/dist/src/keys/ed25519-browser.js.map +1 -1
- package/dist/src/keys/ed25519-class.d.ts +4 -3
- package/dist/src/keys/ed25519-class.d.ts.map +1 -1
- package/dist/src/keys/ed25519-class.js +20 -9
- package/dist/src/keys/ed25519-class.js.map +1 -1
- package/dist/src/keys/ed25519.d.ts +5 -4
- package/dist/src/keys/ed25519.d.ts.map +1 -1
- package/dist/src/keys/ed25519.js +11 -19
- package/dist/src/keys/ed25519.js.map +1 -1
- package/dist/src/keys/index.d.ts +1 -1
- package/dist/src/keys/index.d.ts.map +1 -1
- package/dist/src/keys/index.js +1 -1
- package/dist/src/keys/index.js.map +1 -1
- package/dist/src/keys/key-stretcher.js +1 -1
- package/dist/src/keys/key-stretcher.js.map +1 -1
- package/dist/src/keys/keys.js.map +1 -1
- package/dist/src/keys/rsa-browser.d.ts +5 -4
- package/dist/src/keys/rsa-browser.d.ts.map +1 -1
- package/dist/src/keys/rsa-browser.js +4 -4
- package/dist/src/keys/rsa-browser.js.map +1 -1
- package/dist/src/keys/rsa-class.d.ts +8 -7
- package/dist/src/keys/rsa-class.d.ts.map +1 -1
- package/dist/src/keys/rsa-class.js +16 -9
- package/dist/src/keys/rsa-class.js.map +1 -1
- package/dist/src/keys/rsa-utils.js +1 -1
- package/dist/src/keys/rsa-utils.js.map +1 -1
- package/dist/src/keys/rsa.d.ts +5 -4
- package/dist/src/keys/rsa.d.ts.map +1 -1
- package/dist/src/keys/rsa.js +39 -13
- package/dist/src/keys/rsa.js.map +1 -1
- package/dist/src/keys/secp256k1-browser.d.ts +18 -0
- package/dist/src/keys/secp256k1-browser.d.ts.map +1 -0
- package/dist/src/keys/secp256k1-browser.js +78 -0
- package/dist/src/keys/secp256k1-browser.js.map +1 -0
- package/dist/src/keys/secp256k1-class.d.ts +4 -3
- package/dist/src/keys/secp256k1-class.d.ts.map +1 -1
- package/dist/src/keys/secp256k1-class.js +18 -7
- package/dist/src/keys/secp256k1-class.js.map +1 -1
- package/dist/src/keys/secp256k1.d.ts +3 -2
- package/dist/src/keys/secp256k1.d.ts.map +1 -1
- package/dist/src/keys/secp256k1.js +24 -6
- package/dist/src/keys/secp256k1.js.map +1 -1
- package/dist/src/pbkdf2.js +1 -1
- package/dist/src/pbkdf2.js.map +1 -1
- package/dist/src/random-bytes.js +1 -1
- package/dist/src/random-bytes.js.map +1 -1
- package/dist/src/util.d.ts +1 -0
- package/dist/src/util.d.ts.map +1 -1
- package/dist/src/util.js +8 -0
- package/dist/src/util.js.map +1 -1
- package/dist/src/webcrypto.js +1 -1
- package/dist/src/webcrypto.js.map +1 -1
- package/package.json +11 -6
- package/src/aes/cipher-mode.ts +1 -1
- package/src/aes/index.ts +1 -1
- package/src/ciphers/aes-gcm.ts +3 -3
- package/src/keys/ecdh-browser.ts +1 -1
- package/src/keys/ecdh.ts +1 -1
- package/src/keys/ed25519-browser.ts +7 -6
- package/src/keys/ed25519-class.ts +22 -9
- package/src/keys/ed25519.ts +12 -20
- package/src/keys/index.ts +2 -2
- package/src/keys/key-stretcher.ts +1 -1
- package/src/keys/rsa-browser.ts +10 -9
- package/src/keys/rsa-class.ts +22 -12
- package/src/keys/rsa-utils.ts +1 -1
- package/src/keys/rsa.ts +44 -17
- package/src/keys/secp256k1-browser.ts +87 -0
- package/src/keys/secp256k1-class.ts +20 -7
- package/src/keys/secp256k1.ts +29 -6
- package/src/pbkdf2.ts +1 -1
- package/src/random-bytes.ts +1 -1
- package/src/util.ts +10 -0
- package/src/webcrypto.ts +1 -1
package/src/keys/rsa-class.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
2
2
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
3
|
// @ts-expect-error types are missing
|
|
4
4
|
import forge from 'node-forge/lib/forge.js'
|
|
5
5
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
6
6
|
import 'node-forge/lib/sha512.js'
|
|
7
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
|
+
import { isPromise } from '../util.js'
|
|
8
9
|
import { exporter } from './exporter.js'
|
|
9
10
|
import * as pbm from './keys.js'
|
|
10
11
|
import * as crypto from './rsa.js'
|
|
11
12
|
import type { Multibase } from 'multiformats'
|
|
13
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
12
14
|
|
|
13
15
|
export const MAX_KEY_SIZE = 8192
|
|
14
16
|
|
|
@@ -19,7 +21,7 @@ export class RsaPublicKey {
|
|
|
19
21
|
this._key = key
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean> {
|
|
23
25
|
return crypto.hashAndVerify(this._key, sig, data)
|
|
24
26
|
}
|
|
25
27
|
|
|
@@ -34,18 +36,22 @@ export class RsaPublicKey {
|
|
|
34
36
|
}).subarray()
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
encrypt (bytes: Uint8Array): Uint8Array {
|
|
39
|
+
encrypt (bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
38
40
|
return crypto.encrypt(this._key, bytes)
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
equals (key: any): boolean {
|
|
43
|
+
equals (key: any): boolean | boolean {
|
|
42
44
|
return uint8ArrayEquals(this.bytes, key.bytes)
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
+
hash (): Uint8Array | Promise<Uint8Array> {
|
|
48
|
+
const p = sha256.digest(this.bytes)
|
|
49
|
+
|
|
50
|
+
if (isPromise(p)) {
|
|
51
|
+
return p.then(({ bytes }) => bytes)
|
|
52
|
+
}
|
|
47
53
|
|
|
48
|
-
return bytes
|
|
54
|
+
return p.bytes
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
57
|
|
|
@@ -62,7 +68,7 @@ export class RsaPrivateKey {
|
|
|
62
68
|
return crypto.getRandomValues(16)
|
|
63
69
|
}
|
|
64
70
|
|
|
65
|
-
|
|
71
|
+
sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
66
72
|
return crypto.hashAndSign(this._key, message)
|
|
67
73
|
}
|
|
68
74
|
|
|
@@ -74,7 +80,7 @@ export class RsaPrivateKey {
|
|
|
74
80
|
return new RsaPublicKey(this._publicKey)
|
|
75
81
|
}
|
|
76
82
|
|
|
77
|
-
decrypt (bytes: Uint8Array): Uint8Array {
|
|
83
|
+
decrypt (bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
78
84
|
return crypto.decrypt(this._key, bytes)
|
|
79
85
|
}
|
|
80
86
|
|
|
@@ -93,10 +99,14 @@ export class RsaPrivateKey {
|
|
|
93
99
|
return uint8ArrayEquals(this.bytes, key.bytes)
|
|
94
100
|
}
|
|
95
101
|
|
|
96
|
-
|
|
97
|
-
const
|
|
102
|
+
hash (): Uint8Array | Promise<Uint8Array> {
|
|
103
|
+
const p = sha256.digest(this.bytes)
|
|
104
|
+
|
|
105
|
+
if (isPromise(p)) {
|
|
106
|
+
return p.then(({ bytes }) => bytes)
|
|
107
|
+
}
|
|
98
108
|
|
|
99
|
-
return bytes
|
|
109
|
+
return p.bytes
|
|
100
110
|
}
|
|
101
111
|
|
|
102
112
|
/**
|
package/src/keys/rsa-utils.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import 'node-forge/lib/asn1.js'
|
|
2
2
|
import 'node-forge/lib/rsa.js'
|
|
3
|
-
import { CodeError } from '@libp2p/interface
|
|
3
|
+
import { CodeError } from '@libp2p/interface'
|
|
4
4
|
// @ts-expect-error types are missing
|
|
5
5
|
import forge from 'node-forge/lib/forge.js'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
package/src/keys/rsa.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import crypto from 'crypto'
|
|
2
2
|
import { promisify } from 'util'
|
|
3
|
-
import { CodeError } from '@libp2p/interface
|
|
3
|
+
import { CodeError } from '@libp2p/interface'
|
|
4
4
|
import randomBytes from '../random-bytes.js'
|
|
5
5
|
import * as utils from './rsa-utils.js'
|
|
6
6
|
import type { JWKKeyPair } from './interface.js'
|
|
7
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
7
8
|
|
|
8
9
|
const keypair = promisify(crypto.generateKeyPair)
|
|
9
10
|
|
|
@@ -42,30 +43,56 @@ export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair>
|
|
|
42
43
|
|
|
43
44
|
export { randomBytes as getRandomValues }
|
|
44
45
|
|
|
45
|
-
export async function hashAndSign (key: JsonWebKey, msg: Uint8Array): Promise<Uint8Array> {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
.
|
|
46
|
+
export async function hashAndSign (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
47
|
+
const hash = crypto.createSign('RSA-SHA256')
|
|
48
|
+
|
|
49
|
+
if (msg instanceof Uint8Array) {
|
|
50
|
+
hash.update(msg)
|
|
51
|
+
} else {
|
|
52
|
+
for (const buf of msg) {
|
|
53
|
+
hash.update(buf)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
58
|
+
return hash.sign({ format: 'jwk', key })
|
|
50
59
|
}
|
|
51
60
|
|
|
52
|
-
export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.
|
|
61
|
+
export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
|
|
62
|
+
const hash = crypto.createVerify('RSA-SHA256')
|
|
63
|
+
|
|
64
|
+
if (msg instanceof Uint8Array) {
|
|
65
|
+
hash.update(msg)
|
|
66
|
+
} else {
|
|
67
|
+
for (const buf of msg) {
|
|
68
|
+
hash.update(buf)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
73
|
+
return hash.verify({ format: 'jwk', key }, sig)
|
|
57
74
|
}
|
|
58
75
|
|
|
59
76
|
const padding = crypto.constants.RSA_PKCS1_PADDING
|
|
60
77
|
|
|
61
|
-
export function encrypt (key: JsonWebKey, bytes: Uint8Array): Uint8Array {
|
|
62
|
-
|
|
63
|
-
|
|
78
|
+
export function encrypt (key: JsonWebKey, bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
79
|
+
if (bytes instanceof Uint8Array) {
|
|
80
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
81
|
+
return crypto.publicEncrypt({ format: 'jwk', key, padding }, bytes)
|
|
82
|
+
} else {
|
|
83
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
84
|
+
return crypto.publicEncrypt({ format: 'jwk', key, padding }, bytes.subarray())
|
|
85
|
+
}
|
|
64
86
|
}
|
|
65
87
|
|
|
66
|
-
export function decrypt (key: JsonWebKey, bytes: Uint8Array): Uint8Array {
|
|
67
|
-
|
|
68
|
-
|
|
88
|
+
export function decrypt (key: JsonWebKey, bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
89
|
+
if (bytes instanceof Uint8Array) {
|
|
90
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
91
|
+
return crypto.privateDecrypt({ format: 'jwk', key, padding }, bytes)
|
|
92
|
+
} else {
|
|
93
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
94
|
+
return crypto.privateDecrypt({ format: 'jwk', key, padding }, bytes.subarray())
|
|
95
|
+
}
|
|
69
96
|
}
|
|
70
97
|
|
|
71
98
|
export function keySize (jwk: JsonWebKey): number {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
2
|
+
import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
3
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
|
4
|
+
import { isPromise } from '../util.js'
|
|
5
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
|
+
|
|
7
|
+
const PRIVATE_KEY_BYTE_LENGTH = 32
|
|
8
|
+
|
|
9
|
+
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
10
|
+
|
|
11
|
+
export function generateKey (): Uint8Array {
|
|
12
|
+
return secp.utils.randomPrivateKey()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Hash and sign message with private key
|
|
17
|
+
*/
|
|
18
|
+
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
19
|
+
const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
20
|
+
|
|
21
|
+
if (isPromise(p)) {
|
|
22
|
+
return p.then(({ digest }) => secp.sign(digest, key).toDERRawBytes())
|
|
23
|
+
.catch(err => {
|
|
24
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
return secp.sign(p.digest, key).toDERRawBytes()
|
|
30
|
+
} catch (err) {
|
|
31
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Hash message and verify signature with public key
|
|
37
|
+
*/
|
|
38
|
+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean | Promise<boolean> {
|
|
39
|
+
const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
40
|
+
|
|
41
|
+
if (isPromise(p)) {
|
|
42
|
+
return p.then(({ digest }) => secp.verify(sig, digest, key))
|
|
43
|
+
.catch(err => {
|
|
44
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
return secp.verify(sig, p.digest, key)
|
|
50
|
+
} catch (err) {
|
|
51
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function compressPublicKey (key: Uint8Array): Uint8Array {
|
|
56
|
+
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(true)
|
|
57
|
+
return point
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function decompressPublicKey (key: Uint8Array): Uint8Array {
|
|
61
|
+
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(false)
|
|
62
|
+
return point
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function validatePrivateKey (key: Uint8Array): void {
|
|
66
|
+
try {
|
|
67
|
+
secp.getPublicKey(key, true)
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function validatePublicKey (key: Uint8Array): void {
|
|
74
|
+
try {
|
|
75
|
+
secp.ProjectivePoint.fromHex(key)
|
|
76
|
+
} catch (err) {
|
|
77
|
+
throw new CodeError(String(err), 'ERR_INVALID_PUBLIC_KEY')
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function computePublicKey (privateKey: Uint8Array): Uint8Array {
|
|
82
|
+
try {
|
|
83
|
+
return secp.getPublicKey(privateKey, true)
|
|
84
|
+
} catch (err) {
|
|
85
|
+
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
2
2
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
3
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
4
4
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
5
|
+
import { isPromise } from '../util.js'
|
|
5
6
|
import { exporter } from './exporter.js'
|
|
6
7
|
import * as keysProtobuf from './keys.js'
|
|
7
8
|
import * as crypto from './secp256k1.js'
|
|
8
9
|
import type { Multibase } from 'multiformats'
|
|
10
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
11
|
|
|
10
12
|
export class Secp256k1PublicKey {
|
|
11
13
|
private readonly _key: Uint8Array
|
|
@@ -15,7 +17,7 @@ export class Secp256k1PublicKey {
|
|
|
15
17
|
this._key = key
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean {
|
|
19
21
|
return crypto.hashAndVerify(this._key, sig, data)
|
|
20
22
|
}
|
|
21
23
|
|
|
@@ -35,7 +37,14 @@ export class Secp256k1PublicKey {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
async hash (): Promise<Uint8Array> {
|
|
38
|
-
const
|
|
40
|
+
const p = sha256.digest(this.bytes)
|
|
41
|
+
let bytes: Uint8Array
|
|
42
|
+
|
|
43
|
+
if (isPromise(p)) {
|
|
44
|
+
({ bytes } = await p)
|
|
45
|
+
} else {
|
|
46
|
+
bytes = p.bytes
|
|
47
|
+
}
|
|
39
48
|
|
|
40
49
|
return bytes
|
|
41
50
|
}
|
|
@@ -52,7 +61,7 @@ export class Secp256k1PrivateKey {
|
|
|
52
61
|
crypto.validatePublicKey(this._publicKey)
|
|
53
62
|
}
|
|
54
63
|
|
|
55
|
-
|
|
64
|
+
sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
56
65
|
return crypto.hashAndSign(this._key, message)
|
|
57
66
|
}
|
|
58
67
|
|
|
@@ -75,10 +84,14 @@ export class Secp256k1PrivateKey {
|
|
|
75
84
|
return uint8ArrayEquals(this.bytes, key.bytes)
|
|
76
85
|
}
|
|
77
86
|
|
|
78
|
-
|
|
79
|
-
const
|
|
87
|
+
hash (): Uint8Array | Promise<Uint8Array> {
|
|
88
|
+
const p = sha256.digest(this.bytes)
|
|
80
89
|
|
|
81
|
-
|
|
90
|
+
if (isPromise(p)) {
|
|
91
|
+
return p.then(({ bytes }) => bytes)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return p.bytes
|
|
82
95
|
}
|
|
83
96
|
|
|
84
97
|
/**
|
package/src/keys/secp256k1.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import crypto from 'node:crypto'
|
|
2
|
+
import { CodeError } from '@libp2p/interface'
|
|
2
3
|
import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
3
|
-
import {
|
|
4
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
4
5
|
|
|
5
6
|
const PRIVATE_KEY_BYTE_LENGTH = 32
|
|
6
7
|
|
|
@@ -13,8 +14,19 @@ export function generateKey (): Uint8Array {
|
|
|
13
14
|
/**
|
|
14
15
|
* Hash and sign message with private key
|
|
15
16
|
*/
|
|
16
|
-
export
|
|
17
|
-
const
|
|
17
|
+
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
18
|
+
const hash = crypto.createHash('sha256')
|
|
19
|
+
|
|
20
|
+
if (msg instanceof Uint8Array) {
|
|
21
|
+
hash.update(msg)
|
|
22
|
+
} else {
|
|
23
|
+
for (const buf of msg) {
|
|
24
|
+
hash.update(buf)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const digest = hash.digest()
|
|
29
|
+
|
|
18
30
|
try {
|
|
19
31
|
const signature = secp.sign(digest, key)
|
|
20
32
|
return signature.toDERRawBytes()
|
|
@@ -26,9 +38,20 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Ui
|
|
|
26
38
|
/**
|
|
27
39
|
* Hash message and verify signature with public key
|
|
28
40
|
*/
|
|
29
|
-
export
|
|
41
|
+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
|
|
42
|
+
const hash = crypto.createHash('sha256')
|
|
43
|
+
|
|
44
|
+
if (msg instanceof Uint8Array) {
|
|
45
|
+
hash.update(msg)
|
|
46
|
+
} else {
|
|
47
|
+
for (const buf of msg) {
|
|
48
|
+
hash.update(buf)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const digest = hash.digest()
|
|
53
|
+
|
|
30
54
|
try {
|
|
31
|
-
const { digest } = await sha256.digest(msg)
|
|
32
55
|
return secp.verify(sig, digest, key)
|
|
33
56
|
} catch (err) {
|
|
34
57
|
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
package/src/pbkdf2.ts
CHANGED
package/src/random-bytes.ts
CHANGED
package/src/util.ts
CHANGED
|
@@ -40,3 +40,13 @@ export function base64urlToBuffer (str: string, len?: number): Uint8Array {
|
|
|
40
40
|
|
|
41
41
|
return buf
|
|
42
42
|
}
|
|
43
|
+
|
|
44
|
+
export function isPromise <T = unknown> (thing: any): thing is Promise<T> {
|
|
45
|
+
if (thing == null) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return typeof thing.then === 'function' &&
|
|
50
|
+
typeof thing.catch === 'function' &&
|
|
51
|
+
typeof thing.finally === 'function'
|
|
52
|
+
}
|
package/src/webcrypto.ts
CHANGED
|
@@ -13,7 +13,7 @@ export default {
|
|
|
13
13
|
'The most likely cause of this error is that this page is being accessed ' +
|
|
14
14
|
'from an insecure context (i.e. not HTTPS). For more information and ' +
|
|
15
15
|
'possible resolutions see ' +
|
|
16
|
-
'https://github.com/libp2p/js-libp2p
|
|
16
|
+
'https://github.com/libp2p/js-libp2p/blob/main/packages/crypto/README.md#web-crypto-api'
|
|
17
17
|
),
|
|
18
18
|
{ code: 'ERR_MISSING_WEB_CRYPTO' }
|
|
19
19
|
)
|