@gjsify/crypto 0.1.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 +27 -0
- package/lib/esm/asn1.js +504 -0
- package/lib/esm/bigint-math.js +34 -0
- package/lib/esm/cipher.js +1272 -0
- package/lib/esm/constants.js +15 -0
- package/lib/esm/crypto-utils.js +47 -0
- package/lib/esm/dh.js +411 -0
- package/lib/esm/ecdh.js +356 -0
- package/lib/esm/ecdsa.js +125 -0
- package/lib/esm/hash.js +100 -0
- package/lib/esm/hkdf.js +58 -0
- package/lib/esm/hmac.js +93 -0
- package/lib/esm/index.js +158 -0
- package/lib/esm/key-object.js +330 -0
- package/lib/esm/mgf1.js +27 -0
- package/lib/esm/pbkdf2.js +68 -0
- package/lib/esm/public-encrypt.js +175 -0
- package/lib/esm/random.js +138 -0
- package/lib/esm/rsa-oaep.js +95 -0
- package/lib/esm/rsa-pss.js +100 -0
- package/lib/esm/scrypt.js +134 -0
- package/lib/esm/sign.js +248 -0
- package/lib/esm/timing-safe-equal.js +13 -0
- package/lib/esm/x509.js +214 -0
- package/lib/types/asn1.d.ts +87 -0
- package/lib/types/bigint-math.d.ts +13 -0
- package/lib/types/cipher.d.ts +84 -0
- package/lib/types/constants.d.ts +10 -0
- package/lib/types/crypto-utils.d.ts +22 -0
- package/lib/types/dh.d.ts +79 -0
- package/lib/types/ecdh.d.ts +96 -0
- package/lib/types/ecdsa.d.ts +21 -0
- package/lib/types/hash.d.ts +25 -0
- package/lib/types/hkdf.d.ts +9 -0
- package/lib/types/hmac.d.ts +20 -0
- package/lib/types/index.d.ts +105 -0
- package/lib/types/key-object.d.ts +36 -0
- package/lib/types/mgf1.d.ts +5 -0
- package/lib/types/pbkdf2.d.ts +9 -0
- package/lib/types/public-encrypt.d.ts +42 -0
- package/lib/types/random.d.ts +22 -0
- package/lib/types/rsa-oaep.d.ts +8 -0
- package/lib/types/rsa-pss.d.ts +8 -0
- package/lib/types/scrypt.d.ts +11 -0
- package/lib/types/sign.d.ts +61 -0
- package/lib/types/timing-safe-equal.d.ts +6 -0
- package/lib/types/x509.d.ts +72 -0
- package/package.json +45 -0
- package/src/asn1.ts +797 -0
- package/src/bigint-math.ts +45 -0
- package/src/cipher.spec.ts +332 -0
- package/src/cipher.ts +952 -0
- package/src/constants.ts +16 -0
- package/src/crypto-utils.ts +64 -0
- package/src/dh.spec.ts +111 -0
- package/src/dh.ts +761 -0
- package/src/ecdh.spec.ts +116 -0
- package/src/ecdh.ts +624 -0
- package/src/ecdsa.ts +243 -0
- package/src/extended.spec.ts +444 -0
- package/src/gcm.spec.ts +141 -0
- package/src/hash.spec.ts +86 -0
- package/src/hash.ts +119 -0
- package/src/hkdf.ts +99 -0
- package/src/hmac.spec.ts +64 -0
- package/src/hmac.ts +123 -0
- package/src/index.ts +93 -0
- package/src/key-object.spec.ts +202 -0
- package/src/key-object.ts +401 -0
- package/src/mgf1.ts +37 -0
- package/src/pbkdf2.spec.ts +76 -0
- package/src/pbkdf2.ts +106 -0
- package/src/public-encrypt.ts +288 -0
- package/src/random.spec.ts +133 -0
- package/src/random.ts +183 -0
- package/src/rsa-oaep.ts +167 -0
- package/src/rsa-pss.ts +190 -0
- package/src/scrypt.spec.ts +90 -0
- package/src/scrypt.ts +191 -0
- package/src/sign.spec.ts +160 -0
- package/src/sign.ts +319 -0
- package/src/test.mts +19 -0
- package/src/timing-safe-equal.ts +21 -0
- package/src/x509.spec.ts +210 -0
- package/src/x509.ts +262 -0
- package/tsconfig.json +31 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
export declare class DiffieHellman {
|
|
3
|
+
private _prime;
|
|
4
|
+
private _generator;
|
|
5
|
+
private _generatorBuf;
|
|
6
|
+
private _primeByteLength;
|
|
7
|
+
private _pubKey;
|
|
8
|
+
private _privKey;
|
|
9
|
+
private _primeCode;
|
|
10
|
+
private _malleable;
|
|
11
|
+
constructor(prime: Buffer, generator: Buffer, malleable?: boolean);
|
|
12
|
+
/**
|
|
13
|
+
* Error code from prime/generator validation.
|
|
14
|
+
* Lazily computed on first access.
|
|
15
|
+
*/
|
|
16
|
+
get verifyError(): number;
|
|
17
|
+
/**
|
|
18
|
+
* Generate a random private key and compute the corresponding public key.
|
|
19
|
+
* Returns the public key as a Buffer (or encoded string).
|
|
20
|
+
*/
|
|
21
|
+
generateKeys(): Buffer;
|
|
22
|
+
generateKeys(encoding: BufferEncoding): string;
|
|
23
|
+
/**
|
|
24
|
+
* Compute the shared secret using the other party's public key.
|
|
25
|
+
*/
|
|
26
|
+
computeSecret(otherPublicKey: Buffer | Uint8Array | string, inputEncoding?: BufferEncoding): Buffer;
|
|
27
|
+
computeSecret(otherPublicKey: Buffer | Uint8Array | string, inputEncoding: BufferEncoding, outputEncoding: BufferEncoding): string;
|
|
28
|
+
/**
|
|
29
|
+
* Get the prime as a Buffer or encoded string.
|
|
30
|
+
*/
|
|
31
|
+
getPrime(): Buffer;
|
|
32
|
+
getPrime(encoding: BufferEncoding): string;
|
|
33
|
+
/**
|
|
34
|
+
* Get the generator as a Buffer or encoded string.
|
|
35
|
+
*/
|
|
36
|
+
getGenerator(): Buffer;
|
|
37
|
+
getGenerator(encoding: BufferEncoding): string;
|
|
38
|
+
/**
|
|
39
|
+
* Get the public key as a Buffer or encoded string.
|
|
40
|
+
*/
|
|
41
|
+
getPublicKey(): Buffer;
|
|
42
|
+
getPublicKey(encoding: BufferEncoding): string;
|
|
43
|
+
/**
|
|
44
|
+
* Get the private key as a Buffer or encoded string.
|
|
45
|
+
*/
|
|
46
|
+
getPrivateKey(): Buffer;
|
|
47
|
+
getPrivateKey(encoding: BufferEncoding): string;
|
|
48
|
+
/**
|
|
49
|
+
* Set the public key. Only available for malleable DH instances
|
|
50
|
+
* (created via createDiffieHellman, not getDiffieHellman).
|
|
51
|
+
*/
|
|
52
|
+
setPublicKey(publicKey: Buffer | Uint8Array | string, encoding?: BufferEncoding): void;
|
|
53
|
+
/**
|
|
54
|
+
* Set the private key. Only available for malleable DH instances
|
|
55
|
+
* (created via createDiffieHellman, not getDiffieHellman).
|
|
56
|
+
*/
|
|
57
|
+
setPrivateKey(privateKey: Buffer | Uint8Array | string, encoding?: BufferEncoding): void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create a DiffieHellman key exchange object.
|
|
61
|
+
*
|
|
62
|
+
* Usage:
|
|
63
|
+
* createDiffieHellman(primeLength)
|
|
64
|
+
* createDiffieHellman(primeLength, generator)
|
|
65
|
+
* createDiffieHellman(prime, primeEncoding, generator, generatorEncoding)
|
|
66
|
+
* createDiffieHellman(prime, generator)
|
|
67
|
+
*/
|
|
68
|
+
export declare function createDiffieHellman(prime: number | string | Buffer | Uint8Array, primeEncoding?: BufferEncoding | number | string | Buffer | Uint8Array, generator?: number | string | Buffer | Uint8Array, generatorEncoding?: BufferEncoding): DiffieHellman;
|
|
69
|
+
/**
|
|
70
|
+
* Create a DiffieHellman key exchange object using a predefined MODP group.
|
|
71
|
+
*
|
|
72
|
+
* Note: Instances from getDiffieHellman do NOT have setPublicKey/setPrivateKey,
|
|
73
|
+
* matching Node.js behavior where predefined group instances are not malleable.
|
|
74
|
+
*/
|
|
75
|
+
export declare function getDiffieHellman(groupName: string): DiffieHellman;
|
|
76
|
+
/** Alias for getDiffieHellman. */
|
|
77
|
+
export declare const createDiffieHellmanGroup: typeof getDiffieHellman;
|
|
78
|
+
/** Alias for getDiffieHellman. */
|
|
79
|
+
export declare const DiffieHellmanGroup: typeof getDiffieHellman;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/** A point on an elliptic curve, or null for the point at infinity. */
|
|
3
|
+
type ECPoint = {
|
|
4
|
+
x: bigint;
|
|
5
|
+
y: bigint;
|
|
6
|
+
} | null;
|
|
7
|
+
interface CurveParams {
|
|
8
|
+
/** Field prime */
|
|
9
|
+
p: bigint;
|
|
10
|
+
/** Curve coefficient a */
|
|
11
|
+
a: bigint;
|
|
12
|
+
/** Curve coefficient b */
|
|
13
|
+
b: bigint;
|
|
14
|
+
/** Generator x-coordinate */
|
|
15
|
+
Gx: bigint;
|
|
16
|
+
/** Generator y-coordinate */
|
|
17
|
+
Gy: bigint;
|
|
18
|
+
/** Order of the generator */
|
|
19
|
+
n: bigint;
|
|
20
|
+
/** Byte length of a field element */
|
|
21
|
+
byteLength: number;
|
|
22
|
+
}
|
|
23
|
+
declare const CURVES: Record<string, CurveParams>;
|
|
24
|
+
declare const CURVE_ALIASES: Record<string, string>;
|
|
25
|
+
/**
|
|
26
|
+
* Non-negative modulus: always returns a value in [0, mod).
|
|
27
|
+
*/
|
|
28
|
+
declare function mod(a: bigint, m: bigint): bigint;
|
|
29
|
+
/**
|
|
30
|
+
* Modular multiplicative inverse using extended Euclidean algorithm.
|
|
31
|
+
* Returns x such that (a * x) mod m = 1.
|
|
32
|
+
* Throws if a and m are not coprime.
|
|
33
|
+
*/
|
|
34
|
+
declare function modInverse(a: bigint, m: bigint): bigint;
|
|
35
|
+
/**
|
|
36
|
+
* Add two points on the curve.
|
|
37
|
+
* Returns the point at infinity (null) when appropriate.
|
|
38
|
+
*/
|
|
39
|
+
declare function pointAdd(P: ECPoint, Q: ECPoint, curve: CurveParams): ECPoint;
|
|
40
|
+
/**
|
|
41
|
+
* Scalar multiplication using the double-and-add method (constant-time-ish
|
|
42
|
+
* with respect to the bit length of the scalar, but not fully
|
|
43
|
+
* constant-time — acceptable since we are not a production crypto library
|
|
44
|
+
* and match the behaviour of refs/create-ecdh which uses elliptic.js).
|
|
45
|
+
*
|
|
46
|
+
* Uses a fixed-window approach of width 1 (Montgomery ladder variant) to
|
|
47
|
+
* avoid the most trivial timing leaks.
|
|
48
|
+
*/
|
|
49
|
+
declare function scalarMul(k: bigint, P: ECPoint, curve: CurveParams): ECPoint;
|
|
50
|
+
declare class ECDH {
|
|
51
|
+
private _curve;
|
|
52
|
+
private _curveName;
|
|
53
|
+
private _privateKey;
|
|
54
|
+
private _publicKey;
|
|
55
|
+
constructor(curveName: string);
|
|
56
|
+
/**
|
|
57
|
+
* Generate a random private key and derive the corresponding public key.
|
|
58
|
+
* Returns the public key.
|
|
59
|
+
*/
|
|
60
|
+
generateKeys(): Buffer;
|
|
61
|
+
generateKeys(encoding: BufferEncoding, format?: 'uncompressed' | 'compressed' | 'hybrid'): string;
|
|
62
|
+
/**
|
|
63
|
+
* Compute the shared secret using the other party's public key.
|
|
64
|
+
*/
|
|
65
|
+
computeSecret(otherPublicKey: string | Buffer | Uint8Array | ArrayBufferView, inputEncoding?: BufferEncoding, outputEncoding?: BufferEncoding): Buffer | string;
|
|
66
|
+
/**
|
|
67
|
+
* Return the public key. Optionally encode as a string.
|
|
68
|
+
*/
|
|
69
|
+
getPublicKey(): Buffer;
|
|
70
|
+
getPublicKey(encoding: BufferEncoding, format?: 'uncompressed' | 'compressed' | 'hybrid'): string;
|
|
71
|
+
/**
|
|
72
|
+
* Return the private key. Optionally encode as a string.
|
|
73
|
+
*/
|
|
74
|
+
getPrivateKey(): Buffer;
|
|
75
|
+
getPrivateKey(encoding: BufferEncoding): string;
|
|
76
|
+
/**
|
|
77
|
+
* Set the public key. The key can be in uncompressed, compressed, or hybrid format.
|
|
78
|
+
*/
|
|
79
|
+
setPublicKey(key: string | Buffer | Uint8Array | ArrayBufferView, encoding?: BufferEncoding): void;
|
|
80
|
+
/**
|
|
81
|
+
* Set the private key and derive the corresponding public key.
|
|
82
|
+
*/
|
|
83
|
+
setPrivateKey(key: string | Buffer | Uint8Array | ArrayBufferView, encoding?: BufferEncoding): void;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create an ECDH key exchange object for the specified curve.
|
|
87
|
+
*
|
|
88
|
+
* Supported curves: secp256k1, prime256v1 (P-256), secp384r1 (P-384), secp521r1 (P-521).
|
|
89
|
+
*/
|
|
90
|
+
export { mod, modInverse, scalarMul, pointAdd, CURVES, CURVE_ALIASES };
|
|
91
|
+
export type { ECPoint, CurveParams };
|
|
92
|
+
export declare function createECDH(curveName: string): ECDH;
|
|
93
|
+
/**
|
|
94
|
+
* Return a list of supported elliptic curve names.
|
|
95
|
+
*/
|
|
96
|
+
export declare function getCurves(): string[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ECDSA signature generation per FIPS 186-4 Section 6.4.
|
|
3
|
+
*
|
|
4
|
+
* @param hashAlgo Hash algorithm name (e.g. 'sha256')
|
|
5
|
+
* @param privKeyBytes Private key as big-endian bytes
|
|
6
|
+
* @param data Data to sign (will be hashed)
|
|
7
|
+
* @param curveName Curve name (e.g. 'P-256')
|
|
8
|
+
* @returns Signature as r || s concatenated (each curve.byteLength bytes)
|
|
9
|
+
*/
|
|
10
|
+
export declare function ecdsaSign(hashAlgo: string, privKeyBytes: Uint8Array, data: Uint8Array, curveName: string): Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* ECDSA signature verification per FIPS 186-4 Section 6.4.
|
|
13
|
+
*
|
|
14
|
+
* @param hashAlgo Hash algorithm name (e.g. 'sha256')
|
|
15
|
+
* @param pubKeyBytes Public key as uncompressed point (0x04 || x || y)
|
|
16
|
+
* @param signature Signature as r || s concatenated
|
|
17
|
+
* @param data Signed data (will be hashed)
|
|
18
|
+
* @param curveName Curve name (e.g. 'P-256')
|
|
19
|
+
* @returns true if signature is valid
|
|
20
|
+
*/
|
|
21
|
+
export declare function ecdsaVerify(hashAlgo: string, pubKeyBytes: Uint8Array, signature: Uint8Array, data: Uint8Array, curveName: string): boolean;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
2
|
+
import type { TransformCallback } from 'node:stream';
|
|
3
|
+
import { Buffer } from 'node:buffer';
|
|
4
|
+
/**
|
|
5
|
+
* Creates and returns a Hash object that can be used to generate hash digests
|
|
6
|
+
* using the given algorithm.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Hash extends Transform {
|
|
9
|
+
private _algorithm;
|
|
10
|
+
private _checksum;
|
|
11
|
+
private _finalized;
|
|
12
|
+
constructor(algorithm: string);
|
|
13
|
+
/** Update the hash with data. */
|
|
14
|
+
update(data: string | Buffer | Uint8Array, inputEncoding?: BufferEncoding): this;
|
|
15
|
+
/** Calculate the digest of all data passed to update(). */
|
|
16
|
+
digest(encoding?: BufferEncoding): Buffer | string;
|
|
17
|
+
/** Copy this hash to a new Hash object. */
|
|
18
|
+
copy(): Hash;
|
|
19
|
+
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
|
|
20
|
+
_flush(callback: TransformCallback): void;
|
|
21
|
+
}
|
|
22
|
+
/** Get the list of supported hash algorithms. */
|
|
23
|
+
export declare function getHashes(): string[];
|
|
24
|
+
/** Convenience: one-shot hash (Node 21+). */
|
|
25
|
+
export declare function hash(algorithm: string, data: string | Buffer | Uint8Array, encoding?: BufferEncoding): Buffer | string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/**
|
|
3
|
+
* Synchronous HKDF key derivation.
|
|
4
|
+
*/
|
|
5
|
+
export declare function hkdfSync(digest: string, ikm: string | Buffer | Uint8Array | DataView | ArrayBuffer, salt: string | Buffer | Uint8Array | DataView | ArrayBuffer, info: string | Buffer | Uint8Array | DataView | ArrayBuffer, keylen: number): ArrayBuffer;
|
|
6
|
+
/**
|
|
7
|
+
* Asynchronous HKDF key derivation.
|
|
8
|
+
*/
|
|
9
|
+
export declare function hkdf(digest: string, ikm: string | Buffer | Uint8Array | DataView | ArrayBuffer, salt: string | Buffer | Uint8Array | DataView | ArrayBuffer, info: string | Buffer | Uint8Array | DataView | ArrayBuffer, keylen: number, callback: (err: Error | null, derivedKey?: ArrayBuffer) => void): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
2
|
+
import type { TransformCallback } from 'node:stream';
|
|
3
|
+
import { Buffer } from 'node:buffer';
|
|
4
|
+
/**
|
|
5
|
+
* Creates and returns an Hmac object that uses the given algorithm and key.
|
|
6
|
+
* Implemented using createHash (GLib.Checksum) since GLib.Hmac bindings are broken in GJS.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Hmac extends Transform {
|
|
9
|
+
private _algorithm;
|
|
10
|
+
private _innerHash;
|
|
11
|
+
private _outerKeyPad;
|
|
12
|
+
private _finalized;
|
|
13
|
+
constructor(algorithm: string, key: string | Buffer | Uint8Array);
|
|
14
|
+
/** Update the HMAC with data. */
|
|
15
|
+
update(data: string | Buffer | Uint8Array, inputEncoding?: BufferEncoding): this;
|
|
16
|
+
/** Calculate the HMAC digest. */
|
|
17
|
+
digest(encoding?: BufferEncoding): Buffer | string;
|
|
18
|
+
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
|
|
19
|
+
_flush(callback: TransformCallback): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export { Hash, getHashes, hash } from './hash.js';
|
|
2
|
+
export { Hmac } from './hmac.js';
|
|
3
|
+
export { randomBytes, randomFill, randomFillSync, randomUUID, randomInt, } from './random.js';
|
|
4
|
+
export { timingSafeEqual } from './timing-safe-equal.js';
|
|
5
|
+
export { constants } from './constants.js';
|
|
6
|
+
export { pbkdf2, pbkdf2Sync } from './pbkdf2.js';
|
|
7
|
+
export { hkdf, hkdfSync } from './hkdf.js';
|
|
8
|
+
export { scrypt, scryptSync } from './scrypt.js';
|
|
9
|
+
import { Hash } from './hash.js';
|
|
10
|
+
import { Hmac } from './hmac.js';
|
|
11
|
+
/** Create a Hash object for the given algorithm. */
|
|
12
|
+
export declare function createHash(algorithm: string): Hash;
|
|
13
|
+
/** Create an Hmac object for the given algorithm and key. */
|
|
14
|
+
export declare function createHmac(algorithm: string, key: string | Buffer | Uint8Array): Hmac;
|
|
15
|
+
export { createCipher, createCipheriv, createDecipher, createDecipheriv, getCiphers } from './cipher.js';
|
|
16
|
+
export { Sign, Verify, createSign, createVerify } from './sign.js';
|
|
17
|
+
export { createDiffieHellman, getDiffieHellman, DiffieHellman, DiffieHellmanGroup, createDiffieHellmanGroup } from './dh.js';
|
|
18
|
+
export { createECDH, getCurves } from './ecdh.js';
|
|
19
|
+
export { ecdsaSign, ecdsaVerify } from './ecdsa.js';
|
|
20
|
+
export { publicEncrypt, privateDecrypt, privateEncrypt, publicDecrypt } from './public-encrypt.js';
|
|
21
|
+
export { rsaPssSign, rsaPssVerify } from './rsa-pss.js';
|
|
22
|
+
export { rsaOaepEncrypt, rsaOaepDecrypt } from './rsa-oaep.js';
|
|
23
|
+
export { mgf1 } from './mgf1.js';
|
|
24
|
+
export { KeyObject, createSecretKey, createPublicKey, createPrivateKey } from './key-object.js';
|
|
25
|
+
export { X509Certificate } from './x509.js';
|
|
26
|
+
import { getHashes, hash } from './hash.js';
|
|
27
|
+
import { randomBytes, randomFill, randomFillSync, randomUUID, randomInt } from './random.js';
|
|
28
|
+
import { timingSafeEqual } from './timing-safe-equal.js';
|
|
29
|
+
import { pbkdf2, pbkdf2Sync } from './pbkdf2.js';
|
|
30
|
+
import { hkdf, hkdfSync } from './hkdf.js';
|
|
31
|
+
import { scrypt, scryptSync } from './scrypt.js';
|
|
32
|
+
import { createCipher, createCipheriv, createDecipher, createDecipheriv, getCiphers } from './cipher.js';
|
|
33
|
+
import { Sign, Verify, createSign, createVerify } from './sign.js';
|
|
34
|
+
import { createDiffieHellman, getDiffieHellman, DiffieHellman } from './dh.js';
|
|
35
|
+
import { createECDH, getCurves } from './ecdh.js';
|
|
36
|
+
import { ecdsaSign, ecdsaVerify } from './ecdsa.js';
|
|
37
|
+
import { publicEncrypt, privateDecrypt, privateEncrypt, publicDecrypt } from './public-encrypt.js';
|
|
38
|
+
import { rsaPssSign, rsaPssVerify } from './rsa-pss.js';
|
|
39
|
+
import { rsaOaepEncrypt, rsaOaepDecrypt } from './rsa-oaep.js';
|
|
40
|
+
import { mgf1 } from './mgf1.js';
|
|
41
|
+
import { KeyObject, createSecretKey, createPublicKey, createPrivateKey } from './key-object.js';
|
|
42
|
+
import { X509Certificate } from './x509.js';
|
|
43
|
+
declare const _default: {
|
|
44
|
+
Hash: typeof Hash;
|
|
45
|
+
getHashes: typeof getHashes;
|
|
46
|
+
hash: typeof hash;
|
|
47
|
+
Hmac: typeof Hmac;
|
|
48
|
+
randomBytes: typeof randomBytes;
|
|
49
|
+
randomFill: typeof randomFill;
|
|
50
|
+
randomFillSync: typeof randomFillSync;
|
|
51
|
+
randomUUID: typeof randomUUID;
|
|
52
|
+
randomInt: typeof randomInt;
|
|
53
|
+
timingSafeEqual: typeof timingSafeEqual;
|
|
54
|
+
constants: {
|
|
55
|
+
readonly RSA_PKCS1_PADDING: 1;
|
|
56
|
+
readonly RSA_NO_PADDING: 3;
|
|
57
|
+
readonly RSA_PKCS1_OAEP_PADDING: 4;
|
|
58
|
+
readonly RSA_PKCS1_PSS_PADDING: 6;
|
|
59
|
+
readonly DH_CHECK_P_NOT_SAFE_PRIME: 2;
|
|
60
|
+
readonly DH_CHECK_P_NOT_PRIME: 1;
|
|
61
|
+
readonly DH_UNABLE_TO_CHECK_GENERATOR: 4;
|
|
62
|
+
readonly DH_NOT_SUITABLE_GENERATOR: 8;
|
|
63
|
+
};
|
|
64
|
+
pbkdf2: typeof pbkdf2;
|
|
65
|
+
pbkdf2Sync: typeof pbkdf2Sync;
|
|
66
|
+
hkdf: typeof hkdf;
|
|
67
|
+
hkdfSync: typeof hkdfSync;
|
|
68
|
+
scrypt: typeof scrypt;
|
|
69
|
+
scryptSync: typeof scryptSync;
|
|
70
|
+
createHash: typeof createHash;
|
|
71
|
+
createHmac: typeof createHmac;
|
|
72
|
+
createCipher: typeof createCipher;
|
|
73
|
+
createCipheriv: typeof createCipheriv;
|
|
74
|
+
createDecipher: typeof createDecipher;
|
|
75
|
+
createDecipheriv: typeof createDecipheriv;
|
|
76
|
+
getCiphers: typeof getCiphers;
|
|
77
|
+
Sign: typeof Sign;
|
|
78
|
+
Verify: typeof Verify;
|
|
79
|
+
createSign: typeof createSign;
|
|
80
|
+
createVerify: typeof createVerify;
|
|
81
|
+
createDiffieHellman: typeof createDiffieHellman;
|
|
82
|
+
getDiffieHellman: typeof getDiffieHellman;
|
|
83
|
+
DiffieHellman: typeof DiffieHellman;
|
|
84
|
+
DiffieHellmanGroup: typeof getDiffieHellman;
|
|
85
|
+
createDiffieHellmanGroup: typeof getDiffieHellman;
|
|
86
|
+
createECDH: typeof createECDH;
|
|
87
|
+
getCurves: typeof getCurves;
|
|
88
|
+
ecdsaSign: typeof ecdsaSign;
|
|
89
|
+
ecdsaVerify: typeof ecdsaVerify;
|
|
90
|
+
publicEncrypt: typeof publicEncrypt;
|
|
91
|
+
privateDecrypt: typeof privateDecrypt;
|
|
92
|
+
privateEncrypt: typeof privateEncrypt;
|
|
93
|
+
publicDecrypt: typeof publicDecrypt;
|
|
94
|
+
rsaPssSign: typeof rsaPssSign;
|
|
95
|
+
rsaPssVerify: typeof rsaPssVerify;
|
|
96
|
+
rsaOaepEncrypt: typeof rsaOaepEncrypt;
|
|
97
|
+
rsaOaepDecrypt: typeof rsaOaepDecrypt;
|
|
98
|
+
mgf1: typeof mgf1;
|
|
99
|
+
KeyObject: typeof KeyObject;
|
|
100
|
+
createSecretKey: typeof createSecretKey;
|
|
101
|
+
createPublicKey: typeof createPublicKey;
|
|
102
|
+
createPrivateKey: typeof createPrivateKey;
|
|
103
|
+
X509Certificate: typeof X509Certificate;
|
|
104
|
+
};
|
|
105
|
+
export default _default;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
export declare class KeyObject {
|
|
3
|
+
readonly type: 'secret' | 'public' | 'private';
|
|
4
|
+
/** @internal */
|
|
5
|
+
_handle: unknown;
|
|
6
|
+
constructor(type: 'secret' | 'public' | 'private', handle: unknown);
|
|
7
|
+
get symmetricKeySize(): number | undefined;
|
|
8
|
+
get asymmetricKeyType(): string | undefined;
|
|
9
|
+
get asymmetricKeySize(): number | undefined;
|
|
10
|
+
equals(otherKeyObject: KeyObject): boolean;
|
|
11
|
+
export(options?: {
|
|
12
|
+
type?: string;
|
|
13
|
+
format?: string;
|
|
14
|
+
}): Buffer | string | object;
|
|
15
|
+
get [Symbol.toStringTag](): string;
|
|
16
|
+
}
|
|
17
|
+
interface KeyInput {
|
|
18
|
+
key: string | Buffer | KeyObject | object;
|
|
19
|
+
format?: 'pem' | 'der' | 'jwk';
|
|
20
|
+
type?: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1';
|
|
21
|
+
passphrase?: string | Buffer;
|
|
22
|
+
encoding?: BufferEncoding;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a secret key from raw bytes.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createSecretKey(key: Buffer | Uint8Array | string, encoding?: BufferEncoding): KeyObject;
|
|
28
|
+
/**
|
|
29
|
+
* Create a public key from PEM, DER, JWK, or another KeyObject.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createPublicKey(key: string | Buffer | KeyInput | KeyObject): KeyObject;
|
|
32
|
+
/**
|
|
33
|
+
* Create a private key from PEM, DER, JWK, or KeyInput.
|
|
34
|
+
*/
|
|
35
|
+
export declare function createPrivateKey(key: string | Buffer | KeyInput): KeyObject;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/**
|
|
3
|
+
* Synchronous PBKDF2 key derivation.
|
|
4
|
+
*/
|
|
5
|
+
export declare function pbkdf2Sync(password: string | Buffer | Uint8Array | DataView, salt: string | Buffer | Uint8Array | DataView, iterations: number, keylen: number, digest?: string): Buffer;
|
|
6
|
+
/**
|
|
7
|
+
* Asynchronous PBKDF2 key derivation.
|
|
8
|
+
*/
|
|
9
|
+
export declare function pbkdf2(password: string | Buffer | Uint8Array | DataView, salt: string | Buffer | Uint8Array | DataView, iterations: number, keylen: number, digest: string, callback: (err: Error | null, derivedKey?: Buffer) => void): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
interface KeyInput {
|
|
3
|
+
key: string | Buffer;
|
|
4
|
+
passphrase?: string;
|
|
5
|
+
padding?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Encrypt data using an RSA public key with PKCS#1 v1.5 Type 2 padding.
|
|
9
|
+
*
|
|
10
|
+
* @param key - PEM-encoded RSA public key (or private key, from which public components are extracted)
|
|
11
|
+
* @param buffer - Data to encrypt (must be <= keyLen - 11 bytes)
|
|
12
|
+
* @returns Encrypted data as a Buffer
|
|
13
|
+
*/
|
|
14
|
+
export declare function publicEncrypt(key: string | Buffer | KeyInput, buffer: Buffer | Uint8Array): Buffer;
|
|
15
|
+
/**
|
|
16
|
+
* Decrypt data using an RSA private key (reverses publicEncrypt).
|
|
17
|
+
*
|
|
18
|
+
* @param key - PEM-encoded RSA private key
|
|
19
|
+
* @param buffer - Encrypted data
|
|
20
|
+
* @returns Decrypted data as a Buffer
|
|
21
|
+
*/
|
|
22
|
+
export declare function privateDecrypt(key: string | Buffer | KeyInput, buffer: Buffer | Uint8Array): Buffer;
|
|
23
|
+
/**
|
|
24
|
+
* Encrypt data using an RSA private key with PKCS#1 v1.5 Type 1 padding.
|
|
25
|
+
* This is the raw RSA private-key operation used for compatibility with
|
|
26
|
+
* signature-like use cases.
|
|
27
|
+
*
|
|
28
|
+
* @param key - PEM-encoded RSA private key
|
|
29
|
+
* @param buffer - Data to encrypt
|
|
30
|
+
* @returns Encrypted data as a Buffer
|
|
31
|
+
*/
|
|
32
|
+
export declare function privateEncrypt(key: string | Buffer | KeyInput, buffer: Buffer | Uint8Array): Buffer;
|
|
33
|
+
/**
|
|
34
|
+
* Decrypt data using an RSA public key (reverses privateEncrypt).
|
|
35
|
+
* Removes PKCS#1 v1.5 Type 1 padding.
|
|
36
|
+
*
|
|
37
|
+
* @param key - PEM-encoded RSA public key (or private key for public components)
|
|
38
|
+
* @param buffer - Encrypted data
|
|
39
|
+
* @returns Decrypted data as a Buffer
|
|
40
|
+
*/
|
|
41
|
+
export declare function publicDecrypt(key: string | Buffer | KeyInput, buffer: Buffer | Uint8Array): Buffer;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/**
|
|
3
|
+
* Generate cryptographically strong pseudo-random data.
|
|
4
|
+
*/
|
|
5
|
+
export declare function randomBytes(size: number): Buffer;
|
|
6
|
+
export declare function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
|
|
7
|
+
/**
|
|
8
|
+
* Fill a buffer with cryptographically strong pseudo-random data (synchronous).
|
|
9
|
+
*/
|
|
10
|
+
export declare function randomFillSync(buffer: Buffer | Uint8Array, offset?: number, size?: number): Buffer | Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* Fill a buffer with cryptographically strong pseudo-random data (async).
|
|
13
|
+
*/
|
|
14
|
+
export declare function randomFill(buffer: Buffer | Uint8Array, offset: number | ((err: Error | null, buf: Buffer | Uint8Array) => void), size?: number | ((err: Error | null, buf: Buffer | Uint8Array) => void), callback?: (err: Error | null, buf: Buffer | Uint8Array) => void): void;
|
|
15
|
+
/**
|
|
16
|
+
* Generate a random UUID v4 string.
|
|
17
|
+
*/
|
|
18
|
+
export declare function randomUUID(): string;
|
|
19
|
+
/**
|
|
20
|
+
* Generate a random integer between min (inclusive) and max (exclusive).
|
|
21
|
+
*/
|
|
22
|
+
export declare function randomInt(min: number, max?: number | ((err: Error | null, value: number) => void), callback?: (err: Error | null, value: number) => void): number | void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RSA-OAEP encrypt using PEM public key.
|
|
3
|
+
*/
|
|
4
|
+
export declare function rsaOaepEncrypt(hashAlgo: string, pubKeyPem: string, plaintext: Uint8Array, label?: Uint8Array): Uint8Array;
|
|
5
|
+
/**
|
|
6
|
+
* RSA-OAEP decrypt using PEM private key.
|
|
7
|
+
*/
|
|
8
|
+
export declare function rsaOaepDecrypt(hashAlgo: string, privKeyPem: string, ciphertext: Uint8Array, label?: Uint8Array): Uint8Array;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RSA-PSS sign using PEM private key.
|
|
3
|
+
*/
|
|
4
|
+
export declare function rsaPssSign(hashAlgo: string, privKeyPem: string, data: Uint8Array, saltLength: number): Uint8Array;
|
|
5
|
+
/**
|
|
6
|
+
* RSA-PSS verify using PEM public key.
|
|
7
|
+
*/
|
|
8
|
+
export declare function rsaPssVerify(hashAlgo: string, pubKeyPem: string, signature: Uint8Array, data: Uint8Array, saltLength: number): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
export interface ScryptOptions {
|
|
3
|
+
N?: number;
|
|
4
|
+
r?: number;
|
|
5
|
+
p?: number;
|
|
6
|
+
maxmem?: number;
|
|
7
|
+
}
|
|
8
|
+
type ScryptCallback = (err: Error | null, derivedKey: Buffer) => void;
|
|
9
|
+
export declare function scryptSync(password: string | Buffer | Uint8Array, salt: string | Buffer | Uint8Array, keylen: number, options?: ScryptOptions): Buffer;
|
|
10
|
+
export declare function scrypt(password: string | Buffer | Uint8Array, salt: string | Buffer | Uint8Array, keylen: number, optionsOrCallback: ScryptOptions | ScryptCallback, callback?: ScryptCallback): void;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
interface KeyInput {
|
|
3
|
+
key: string;
|
|
4
|
+
passphrase?: string;
|
|
5
|
+
padding?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The Sign class generates RSA PKCS#1 v1.5 signatures.
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* const sign = createSign('RSA-SHA256');
|
|
12
|
+
* sign.update('data');
|
|
13
|
+
* const signature = sign.sign(privateKey);
|
|
14
|
+
*/
|
|
15
|
+
export declare class Sign {
|
|
16
|
+
private _algorithm;
|
|
17
|
+
private _hash;
|
|
18
|
+
private _finalized;
|
|
19
|
+
constructor(algorithm: string);
|
|
20
|
+
/**
|
|
21
|
+
* Update the Sign object with the given data.
|
|
22
|
+
*/
|
|
23
|
+
update(data: string | Buffer | Uint8Array, inputEncoding?: BufferEncoding): this;
|
|
24
|
+
/**
|
|
25
|
+
* Compute the signature using the private key.
|
|
26
|
+
* Returns the signature as a Buffer (or string if outputEncoding is given).
|
|
27
|
+
*/
|
|
28
|
+
sign(privateKey: string | Buffer | KeyInput, outputEncoding?: BufferEncoding): Buffer | string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The Verify class verifies RSA PKCS#1 v1.5 signatures.
|
|
32
|
+
*
|
|
33
|
+
* Usage:
|
|
34
|
+
* const verify = createVerify('RSA-SHA256');
|
|
35
|
+
* verify.update('data');
|
|
36
|
+
* const ok = verify.verify(publicKey, signature);
|
|
37
|
+
*/
|
|
38
|
+
export declare class Verify {
|
|
39
|
+
private _algorithm;
|
|
40
|
+
private _hash;
|
|
41
|
+
private _finalized;
|
|
42
|
+
constructor(algorithm: string);
|
|
43
|
+
/**
|
|
44
|
+
* Update the Verify object with the given data.
|
|
45
|
+
*/
|
|
46
|
+
update(data: string | Buffer | Uint8Array, inputEncoding?: BufferEncoding): this;
|
|
47
|
+
/**
|
|
48
|
+
* Verify the signature against the public key.
|
|
49
|
+
* Returns true if the signature is valid, false otherwise.
|
|
50
|
+
*/
|
|
51
|
+
verify(publicKey: string | Buffer | KeyInput, signature: string | Buffer | Uint8Array, signatureEncoding?: BufferEncoding): boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create and return a Sign object for the given algorithm.
|
|
55
|
+
*/
|
|
56
|
+
export declare function createSign(algorithm: string): Sign;
|
|
57
|
+
/**
|
|
58
|
+
* Create and return a Verify object for the given algorithm.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createVerify(algorithm: string): Verify;
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { KeyObject } from './key-object.js';
|
|
3
|
+
/**
|
|
4
|
+
* X509Certificate encapsulates an X.509 certificate and provides
|
|
5
|
+
* read-only access to its information.
|
|
6
|
+
*/
|
|
7
|
+
export declare class X509Certificate {
|
|
8
|
+
private _components;
|
|
9
|
+
private _pem;
|
|
10
|
+
constructor(buf: string | Buffer | Uint8Array);
|
|
11
|
+
/** The DER encoded certificate data. */
|
|
12
|
+
get raw(): Buffer;
|
|
13
|
+
/** The serial number of the certificate as a hex string. */
|
|
14
|
+
get serialNumber(): string;
|
|
15
|
+
/** The subject of the certificate. */
|
|
16
|
+
get subject(): string;
|
|
17
|
+
/** The issuer of the certificate. */
|
|
18
|
+
get issuer(): string;
|
|
19
|
+
/** The start date of the certificate validity period. */
|
|
20
|
+
get validFrom(): string;
|
|
21
|
+
/** The end date of the certificate validity period. */
|
|
22
|
+
get validTo(): string;
|
|
23
|
+
/** SHA-1 fingerprint of the certificate. */
|
|
24
|
+
get fingerprint(): string;
|
|
25
|
+
/** SHA-256 fingerprint of the certificate. */
|
|
26
|
+
get fingerprint256(): string;
|
|
27
|
+
/** SHA-512 fingerprint of the certificate. */
|
|
28
|
+
get fingerprint512(): string;
|
|
29
|
+
/** The public key of the certificate as a KeyObject. */
|
|
30
|
+
get publicKey(): KeyObject;
|
|
31
|
+
/** The Subject Alternative Name extension, if present. */
|
|
32
|
+
get subjectAltName(): string | undefined;
|
|
33
|
+
/** The key usage extension (stub — returns undefined). */
|
|
34
|
+
get keyUsage(): string[] | undefined;
|
|
35
|
+
/** Information access extension (stub — returns undefined). */
|
|
36
|
+
get infoAccess(): string | undefined;
|
|
37
|
+
/** Whether this certificate is a CA certificate. */
|
|
38
|
+
get ca(): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Check whether the certificate matches the given hostname.
|
|
41
|
+
*/
|
|
42
|
+
checkHost(name: string): string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Check whether the certificate matches the given email address.
|
|
45
|
+
*/
|
|
46
|
+
checkEmail(email: string): string | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Check whether the certificate matches the given IP address.
|
|
49
|
+
*/
|
|
50
|
+
checkIP(ip: string): string | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Verify the certificate signature using the given public key.
|
|
53
|
+
*/
|
|
54
|
+
verify(_publicKey: KeyObject): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Check whether this certificate was issued by the given issuer certificate.
|
|
57
|
+
*/
|
|
58
|
+
checkIssued(otherCert: X509Certificate): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Returns a legacy certificate object for compatibility.
|
|
61
|
+
*/
|
|
62
|
+
toLegacyObject(): Record<string, unknown>;
|
|
63
|
+
/**
|
|
64
|
+
* Returns the PEM-encoded certificate.
|
|
65
|
+
*/
|
|
66
|
+
toString(): string;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the PEM-encoded certificate in JSON context.
|
|
69
|
+
*/
|
|
70
|
+
toJSON(): string;
|
|
71
|
+
get [Symbol.toStringTag](): string;
|
|
72
|
+
}
|