@twin.org/crypto 0.0.2-next.3 → 0.0.2-next.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/cjs/index.cjs +3 -215
- package/dist/esm/index.mjs +5 -216
- package/dist/types/index.d.ts +0 -1
- package/docs/changelog.md +36 -0
- package/docs/reference/index.md +0 -1
- package/locales/en.json +1 -1
- package/package.json +5 -4
- package/dist/types/ciphers/rsa.d.ts +0 -63
- package/docs/reference/classes/RSA.md +0 -213
package/dist/cjs/index.cjs
CHANGED
|
@@ -8,7 +8,6 @@ var blake2b = require('@noble/hashes/blake2b');
|
|
|
8
8
|
var bip32 = require('@scure/bip32');
|
|
9
9
|
var slip10_js = require('micro-key-producer/slip10.js');
|
|
10
10
|
var chacha = require('@noble/ciphers/chacha');
|
|
11
|
-
var node_crypto = require('node:crypto');
|
|
12
11
|
var blake3 = require('@noble/hashes/blake3');
|
|
13
12
|
var hmac = require('@noble/hashes/hmac');
|
|
14
13
|
var sha1 = require('@noble/hashes/sha1');
|
|
@@ -208,7 +207,7 @@ class Ed25519 {
|
|
|
208
207
|
// The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20 (0x302e020100300506032b657004220420)
|
|
209
208
|
const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
|
|
210
209
|
const fullKey = core.Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
|
|
211
|
-
return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", true, ["sign"]);
|
|
210
|
+
return crypto.subtle.importKey("pkcs8", new Uint8Array(fullKey), "Ed25519", true, ["sign"]);
|
|
212
211
|
}
|
|
213
212
|
/**
|
|
214
213
|
* Convert a crypto key to raw private key.
|
|
@@ -715,216 +714,6 @@ class ChaCha20Poly1305 {
|
|
|
715
714
|
}
|
|
716
715
|
}
|
|
717
716
|
|
|
718
|
-
// Copyright 2024 IOTA Stiftung.
|
|
719
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
720
|
-
/**
|
|
721
|
-
* Implementation of the RSA cipher.
|
|
722
|
-
*/
|
|
723
|
-
class RSA {
|
|
724
|
-
/**
|
|
725
|
-
* Runtime name for the class.
|
|
726
|
-
* @internal
|
|
727
|
-
*/
|
|
728
|
-
static _CLASS_NAME = "RSA";
|
|
729
|
-
/**
|
|
730
|
-
* The public key for encryption.
|
|
731
|
-
* @internal
|
|
732
|
-
*/
|
|
733
|
-
_publicKey;
|
|
734
|
-
/**
|
|
735
|
-
* The private key for decryption.
|
|
736
|
-
* @internal
|
|
737
|
-
*/
|
|
738
|
-
_privateKey;
|
|
739
|
-
/**
|
|
740
|
-
* The block size for encryption.
|
|
741
|
-
* @internal
|
|
742
|
-
*/
|
|
743
|
-
_blockSize;
|
|
744
|
-
/**
|
|
745
|
-
* The key size for decryption.
|
|
746
|
-
* @internal
|
|
747
|
-
*/
|
|
748
|
-
_keySize;
|
|
749
|
-
/**
|
|
750
|
-
* Create a new instance of RSA.
|
|
751
|
-
* @param publicKey The public key for encryption (DER format as Uint8Array).
|
|
752
|
-
* @param privateKey The private key for decryption (DER format as Uint8Array).
|
|
753
|
-
*/
|
|
754
|
-
constructor(publicKey, privateKey) {
|
|
755
|
-
core.Guards.uint8Array(RSA._CLASS_NAME, "publicKey", publicKey);
|
|
756
|
-
this._publicKey = node_crypto.createPublicKey({
|
|
757
|
-
key: Buffer.from(publicKey),
|
|
758
|
-
format: "der",
|
|
759
|
-
type: "spki"
|
|
760
|
-
});
|
|
761
|
-
if (!core.Is.empty(privateKey)) {
|
|
762
|
-
this._privateKey = node_crypto.createPrivateKey({
|
|
763
|
-
key: Buffer.from(privateKey),
|
|
764
|
-
format: "der",
|
|
765
|
-
type: "pkcs8"
|
|
766
|
-
});
|
|
767
|
-
}
|
|
768
|
-
// Get modulus length in bits from key details
|
|
769
|
-
const modulusLengthBits = this._publicKey.asymmetricKeyDetails?.modulusLength;
|
|
770
|
-
if (core.Is.empty(modulusLengthBits)) {
|
|
771
|
-
throw new core.GeneralError(RSA._CLASS_NAME, "invalidKeySize");
|
|
772
|
-
}
|
|
773
|
-
// Convert bits to bytes
|
|
774
|
-
this._keySize = Math.ceil(modulusLengthBits / 8);
|
|
775
|
-
// Calculate block size for OAEP with SHA-256
|
|
776
|
-
// Formula: keySize - 2 * hashLength - 2
|
|
777
|
-
const hashLength = 32; // SHA-256 = 32 bytes
|
|
778
|
-
// eslint-disable-next-line no-mixed-operators
|
|
779
|
-
this._blockSize = this._keySize - 2 * hashLength - 2;
|
|
780
|
-
}
|
|
781
|
-
/**
|
|
782
|
-
* Generate a new RSA key pair in PKCS8 format.
|
|
783
|
-
* @param modulusLength The key size in bits (default: 2048).
|
|
784
|
-
* @returns The public and private keys as Uint8Array.
|
|
785
|
-
*/
|
|
786
|
-
static generateKeyPair(modulusLength = 2048) {
|
|
787
|
-
const { publicKey, privateKey } = node_crypto.generateKeyPairSync("rsa", {
|
|
788
|
-
modulusLength,
|
|
789
|
-
publicKeyEncoding: {
|
|
790
|
-
type: "spki",
|
|
791
|
-
format: "der"
|
|
792
|
-
},
|
|
793
|
-
privateKeyEncoding: {
|
|
794
|
-
type: "pkcs8",
|
|
795
|
-
format: "der"
|
|
796
|
-
}
|
|
797
|
-
});
|
|
798
|
-
return {
|
|
799
|
-
publicKey: new Uint8Array(publicKey),
|
|
800
|
-
privateKey: new Uint8Array(privateKey)
|
|
801
|
-
};
|
|
802
|
-
}
|
|
803
|
-
/**
|
|
804
|
-
* Convert a PKCS1 key to a PKCS8 key.
|
|
805
|
-
* @param pkcs1Key The PKCS1 key as Uint8Array.
|
|
806
|
-
* @returns The PKCS8 key as Uint8Array.
|
|
807
|
-
*/
|
|
808
|
-
static convertPkcs1ToPkcs8(pkcs1Key) {
|
|
809
|
-
core.Guards.uint8Array(RSA._CLASS_NAME, "pkcs1Key", pkcs1Key);
|
|
810
|
-
const privateKey = node_crypto.createPrivateKey({
|
|
811
|
-
key: Buffer.from(pkcs1Key),
|
|
812
|
-
format: "der",
|
|
813
|
-
type: "pkcs1"
|
|
814
|
-
});
|
|
815
|
-
return new Uint8Array(privateKey.export({
|
|
816
|
-
format: "der",
|
|
817
|
-
type: "pkcs8"
|
|
818
|
-
}));
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Break the private key down in to its components.
|
|
822
|
-
* @param pkcs8Key The PKCS8 key as Uint8Array.
|
|
823
|
-
* @returns The key components.
|
|
824
|
-
*/
|
|
825
|
-
static getPrivateKeyComponents(pkcs8Key) {
|
|
826
|
-
core.Guards.uint8Array(RSA._CLASS_NAME, "pkcs8Key", pkcs8Key);
|
|
827
|
-
const privateKey = node_crypto.createPrivateKey({
|
|
828
|
-
key: Buffer.from(pkcs8Key),
|
|
829
|
-
format: "der",
|
|
830
|
-
type: "pkcs8"
|
|
831
|
-
});
|
|
832
|
-
const jwk = privateKey.export({ format: "jwk" });
|
|
833
|
-
return {
|
|
834
|
-
n: this.base64UrlToBigInt(jwk.n),
|
|
835
|
-
e: this.base64UrlToBigInt(jwk.e),
|
|
836
|
-
d: this.base64UrlToBigInt(jwk.d),
|
|
837
|
-
p: this.base64UrlToBigInt(jwk.p),
|
|
838
|
-
q: this.base64UrlToBigInt(jwk.q),
|
|
839
|
-
dp: this.base64UrlToBigInt(jwk.dp),
|
|
840
|
-
dq: this.base64UrlToBigInt(jwk.dq),
|
|
841
|
-
qi: this.base64UrlToBigInt(jwk.qi)
|
|
842
|
-
};
|
|
843
|
-
}
|
|
844
|
-
/**
|
|
845
|
-
* Break the public key down in to its components.
|
|
846
|
-
* @param spkiKey The SPKI key as Uint8Array.
|
|
847
|
-
* @returns The key components.
|
|
848
|
-
*/
|
|
849
|
-
static getPublicKeyComponents(spkiKey) {
|
|
850
|
-
core.Guards.uint8Array(RSA._CLASS_NAME, "spkiKey", spkiKey);
|
|
851
|
-
const publicKey = node_crypto.createPublicKey({
|
|
852
|
-
key: Buffer.from(spkiKey),
|
|
853
|
-
format: "der",
|
|
854
|
-
type: "spki"
|
|
855
|
-
});
|
|
856
|
-
const jwk = publicKey.export({ format: "jwk" });
|
|
857
|
-
return {
|
|
858
|
-
n: this.base64UrlToBigInt(jwk.n),
|
|
859
|
-
e: this.base64UrlToBigInt(jwk.e)
|
|
860
|
-
};
|
|
861
|
-
}
|
|
862
|
-
/**
|
|
863
|
-
* Convert base64 encoded data to a big int.
|
|
864
|
-
* @param bytes The bytes to convert.
|
|
865
|
-
* @returns The bigint representation of the bytes.
|
|
866
|
-
* @internal
|
|
867
|
-
*/
|
|
868
|
-
static base64UrlToBigInt(base64Url) {
|
|
869
|
-
if (core.Is.empty(base64Url)) {
|
|
870
|
-
return BigInt(0);
|
|
871
|
-
}
|
|
872
|
-
const bytes = core.Converter.base64UrlToBytes(base64Url);
|
|
873
|
-
const hexString = Array.from(bytes)
|
|
874
|
-
.map(byte => byte.toString(16).padStart(2, "0"))
|
|
875
|
-
.join("");
|
|
876
|
-
return BigInt(`0x${hexString}`);
|
|
877
|
-
}
|
|
878
|
-
/**
|
|
879
|
-
* Encrypt the data.
|
|
880
|
-
* @param data The data to encrypt.
|
|
881
|
-
* @returns The data encrypted.
|
|
882
|
-
*/
|
|
883
|
-
encrypt(data) {
|
|
884
|
-
core.Guards.uint8Array(RSA._CLASS_NAME, "data", data);
|
|
885
|
-
if (data.length === 0) {
|
|
886
|
-
return new Uint8Array(0);
|
|
887
|
-
}
|
|
888
|
-
const blocks = [];
|
|
889
|
-
// Split data into blocks of block size
|
|
890
|
-
for (let i = 0; i < data.length; i += this._blockSize) {
|
|
891
|
-
const block = data.slice(i, i + this._blockSize);
|
|
892
|
-
const encryptedBlock = node_crypto.publicEncrypt({
|
|
893
|
-
key: this._publicKey,
|
|
894
|
-
padding: node_crypto.constants.RSA_PKCS1_OAEP_PADDING
|
|
895
|
-
}, block);
|
|
896
|
-
blocks.push(encryptedBlock);
|
|
897
|
-
}
|
|
898
|
-
return core.Uint8ArrayHelper.concat(blocks);
|
|
899
|
-
}
|
|
900
|
-
/**
|
|
901
|
-
* Decrypt the data.
|
|
902
|
-
* @param data The data to decrypt.
|
|
903
|
-
* @returns The data decrypted.
|
|
904
|
-
* @throws GeneralError If no private key is provided.
|
|
905
|
-
*/
|
|
906
|
-
decrypt(data) {
|
|
907
|
-
core.Guards.uint8Array(RSA._CLASS_NAME, "data", data);
|
|
908
|
-
if (core.Is.empty(this._privateKey)) {
|
|
909
|
-
throw new core.GeneralError(RSA._CLASS_NAME, "noPrivateKey");
|
|
910
|
-
}
|
|
911
|
-
if (data.length === 0) {
|
|
912
|
-
return new Uint8Array(0);
|
|
913
|
-
}
|
|
914
|
-
const blocks = [];
|
|
915
|
-
// Split encrypted data into blocks of key size
|
|
916
|
-
for (let i = 0; i < data.length; i += this._keySize) {
|
|
917
|
-
const block = data.slice(i, i + this._keySize);
|
|
918
|
-
const decryptedBlock = node_crypto.privateDecrypt({
|
|
919
|
-
key: this._privateKey,
|
|
920
|
-
padding: node_crypto.constants.RSA_PKCS1_OAEP_PADDING
|
|
921
|
-
}, block);
|
|
922
|
-
blocks.push(decryptedBlock);
|
|
923
|
-
}
|
|
924
|
-
return core.Uint8ArrayHelper.concat(blocks);
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
|
|
928
717
|
// Copyright 2024 IOTA Stiftung.
|
|
929
718
|
// SPDX-License-Identifier: Apache-2.0.
|
|
930
719
|
/**
|
|
@@ -943,7 +732,7 @@ class X25519 {
|
|
|
943
732
|
*/
|
|
944
733
|
static convertPrivateKeyToX25519(ed25519PrivateKey) {
|
|
945
734
|
core.Guards.uint8Array(X25519._CLASS_NAME, "ed25519PrivateKey", ed25519PrivateKey);
|
|
946
|
-
return ed25519.
|
|
735
|
+
return ed25519.ed25519.utils.toMontgomerySecret(ed25519PrivateKey.slice(0, Ed25519.PRIVATE_KEY_SIZE));
|
|
947
736
|
}
|
|
948
737
|
/**
|
|
949
738
|
* Convert Ed25519 public key to X25519 public key.
|
|
@@ -953,7 +742,7 @@ class X25519 {
|
|
|
953
742
|
*/
|
|
954
743
|
static convertPublicKeyToX25519(ed25519PublicKey) {
|
|
955
744
|
core.Guards.uint8Array(X25519._CLASS_NAME, "ed25519PublicKey", ed25519PublicKey);
|
|
956
|
-
return ed25519.
|
|
745
|
+
return ed25519.ed25519.utils.toMontgomery(ed25519PublicKey);
|
|
957
746
|
}
|
|
958
747
|
}
|
|
959
748
|
|
|
@@ -2063,7 +1852,6 @@ exports.PasswordGenerator = PasswordGenerator;
|
|
|
2063
1852
|
exports.PasswordValidator = PasswordValidator;
|
|
2064
1853
|
exports.Pbkdf2 = Pbkdf2;
|
|
2065
1854
|
exports.PemHelper = PemHelper;
|
|
2066
|
-
exports.RSA = RSA;
|
|
2067
1855
|
exports.Secp256k1 = Secp256k1;
|
|
2068
1856
|
exports.Sha1 = Sha1;
|
|
2069
1857
|
exports.Sha256 = Sha256;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { bech32 } from '@scure/base';
|
|
2
2
|
import { Guards, BaseError, GeneralError, Is, Uint8ArrayHelper, Converter, GuardError, Base32, RandomHelper, Validation } from '@twin.org/core';
|
|
3
|
-
import { ed25519
|
|
3
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
4
4
|
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
5
5
|
import { blake2b } from '@noble/hashes/blake2b';
|
|
6
6
|
import { HDKey as HDKey$1 } from '@scure/bip32';
|
|
7
7
|
import { HDKey } from 'micro-key-producer/slip10.js';
|
|
8
8
|
import { chacha20poly1305 } from '@noble/ciphers/chacha';
|
|
9
|
-
import { createPublicKey, createPrivateKey, generateKeyPairSync, publicEncrypt, constants, privateDecrypt } from 'node:crypto';
|
|
10
9
|
import { blake3 } from '@noble/hashes/blake3';
|
|
11
10
|
import { hmac } from '@noble/hashes/hmac';
|
|
12
11
|
import { sha1 } from '@noble/hashes/sha1';
|
|
@@ -186,7 +185,7 @@ class Ed25519 {
|
|
|
186
185
|
// The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20 (0x302e020100300506032b657004220420)
|
|
187
186
|
const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
|
|
188
187
|
const fullKey = Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
|
|
189
|
-
return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", true, ["sign"]);
|
|
188
|
+
return crypto.subtle.importKey("pkcs8", new Uint8Array(fullKey), "Ed25519", true, ["sign"]);
|
|
190
189
|
}
|
|
191
190
|
/**
|
|
192
191
|
* Convert a crypto key to raw private key.
|
|
@@ -693,216 +692,6 @@ class ChaCha20Poly1305 {
|
|
|
693
692
|
}
|
|
694
693
|
}
|
|
695
694
|
|
|
696
|
-
// Copyright 2024 IOTA Stiftung.
|
|
697
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
698
|
-
/**
|
|
699
|
-
* Implementation of the RSA cipher.
|
|
700
|
-
*/
|
|
701
|
-
class RSA {
|
|
702
|
-
/**
|
|
703
|
-
* Runtime name for the class.
|
|
704
|
-
* @internal
|
|
705
|
-
*/
|
|
706
|
-
static _CLASS_NAME = "RSA";
|
|
707
|
-
/**
|
|
708
|
-
* The public key for encryption.
|
|
709
|
-
* @internal
|
|
710
|
-
*/
|
|
711
|
-
_publicKey;
|
|
712
|
-
/**
|
|
713
|
-
* The private key for decryption.
|
|
714
|
-
* @internal
|
|
715
|
-
*/
|
|
716
|
-
_privateKey;
|
|
717
|
-
/**
|
|
718
|
-
* The block size for encryption.
|
|
719
|
-
* @internal
|
|
720
|
-
*/
|
|
721
|
-
_blockSize;
|
|
722
|
-
/**
|
|
723
|
-
* The key size for decryption.
|
|
724
|
-
* @internal
|
|
725
|
-
*/
|
|
726
|
-
_keySize;
|
|
727
|
-
/**
|
|
728
|
-
* Create a new instance of RSA.
|
|
729
|
-
* @param publicKey The public key for encryption (DER format as Uint8Array).
|
|
730
|
-
* @param privateKey The private key for decryption (DER format as Uint8Array).
|
|
731
|
-
*/
|
|
732
|
-
constructor(publicKey, privateKey) {
|
|
733
|
-
Guards.uint8Array(RSA._CLASS_NAME, "publicKey", publicKey);
|
|
734
|
-
this._publicKey = createPublicKey({
|
|
735
|
-
key: Buffer.from(publicKey),
|
|
736
|
-
format: "der",
|
|
737
|
-
type: "spki"
|
|
738
|
-
});
|
|
739
|
-
if (!Is.empty(privateKey)) {
|
|
740
|
-
this._privateKey = createPrivateKey({
|
|
741
|
-
key: Buffer.from(privateKey),
|
|
742
|
-
format: "der",
|
|
743
|
-
type: "pkcs8"
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
// Get modulus length in bits from key details
|
|
747
|
-
const modulusLengthBits = this._publicKey.asymmetricKeyDetails?.modulusLength;
|
|
748
|
-
if (Is.empty(modulusLengthBits)) {
|
|
749
|
-
throw new GeneralError(RSA._CLASS_NAME, "invalidKeySize");
|
|
750
|
-
}
|
|
751
|
-
// Convert bits to bytes
|
|
752
|
-
this._keySize = Math.ceil(modulusLengthBits / 8);
|
|
753
|
-
// Calculate block size for OAEP with SHA-256
|
|
754
|
-
// Formula: keySize - 2 * hashLength - 2
|
|
755
|
-
const hashLength = 32; // SHA-256 = 32 bytes
|
|
756
|
-
// eslint-disable-next-line no-mixed-operators
|
|
757
|
-
this._blockSize = this._keySize - 2 * hashLength - 2;
|
|
758
|
-
}
|
|
759
|
-
/**
|
|
760
|
-
* Generate a new RSA key pair in PKCS8 format.
|
|
761
|
-
* @param modulusLength The key size in bits (default: 2048).
|
|
762
|
-
* @returns The public and private keys as Uint8Array.
|
|
763
|
-
*/
|
|
764
|
-
static generateKeyPair(modulusLength = 2048) {
|
|
765
|
-
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
|
|
766
|
-
modulusLength,
|
|
767
|
-
publicKeyEncoding: {
|
|
768
|
-
type: "spki",
|
|
769
|
-
format: "der"
|
|
770
|
-
},
|
|
771
|
-
privateKeyEncoding: {
|
|
772
|
-
type: "pkcs8",
|
|
773
|
-
format: "der"
|
|
774
|
-
}
|
|
775
|
-
});
|
|
776
|
-
return {
|
|
777
|
-
publicKey: new Uint8Array(publicKey),
|
|
778
|
-
privateKey: new Uint8Array(privateKey)
|
|
779
|
-
};
|
|
780
|
-
}
|
|
781
|
-
/**
|
|
782
|
-
* Convert a PKCS1 key to a PKCS8 key.
|
|
783
|
-
* @param pkcs1Key The PKCS1 key as Uint8Array.
|
|
784
|
-
* @returns The PKCS8 key as Uint8Array.
|
|
785
|
-
*/
|
|
786
|
-
static convertPkcs1ToPkcs8(pkcs1Key) {
|
|
787
|
-
Guards.uint8Array(RSA._CLASS_NAME, "pkcs1Key", pkcs1Key);
|
|
788
|
-
const privateKey = createPrivateKey({
|
|
789
|
-
key: Buffer.from(pkcs1Key),
|
|
790
|
-
format: "der",
|
|
791
|
-
type: "pkcs1"
|
|
792
|
-
});
|
|
793
|
-
return new Uint8Array(privateKey.export({
|
|
794
|
-
format: "der",
|
|
795
|
-
type: "pkcs8"
|
|
796
|
-
}));
|
|
797
|
-
}
|
|
798
|
-
/**
|
|
799
|
-
* Break the private key down in to its components.
|
|
800
|
-
* @param pkcs8Key The PKCS8 key as Uint8Array.
|
|
801
|
-
* @returns The key components.
|
|
802
|
-
*/
|
|
803
|
-
static getPrivateKeyComponents(pkcs8Key) {
|
|
804
|
-
Guards.uint8Array(RSA._CLASS_NAME, "pkcs8Key", pkcs8Key);
|
|
805
|
-
const privateKey = createPrivateKey({
|
|
806
|
-
key: Buffer.from(pkcs8Key),
|
|
807
|
-
format: "der",
|
|
808
|
-
type: "pkcs8"
|
|
809
|
-
});
|
|
810
|
-
const jwk = privateKey.export({ format: "jwk" });
|
|
811
|
-
return {
|
|
812
|
-
n: this.base64UrlToBigInt(jwk.n),
|
|
813
|
-
e: this.base64UrlToBigInt(jwk.e),
|
|
814
|
-
d: this.base64UrlToBigInt(jwk.d),
|
|
815
|
-
p: this.base64UrlToBigInt(jwk.p),
|
|
816
|
-
q: this.base64UrlToBigInt(jwk.q),
|
|
817
|
-
dp: this.base64UrlToBigInt(jwk.dp),
|
|
818
|
-
dq: this.base64UrlToBigInt(jwk.dq),
|
|
819
|
-
qi: this.base64UrlToBigInt(jwk.qi)
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
/**
|
|
823
|
-
* Break the public key down in to its components.
|
|
824
|
-
* @param spkiKey The SPKI key as Uint8Array.
|
|
825
|
-
* @returns The key components.
|
|
826
|
-
*/
|
|
827
|
-
static getPublicKeyComponents(spkiKey) {
|
|
828
|
-
Guards.uint8Array(RSA._CLASS_NAME, "spkiKey", spkiKey);
|
|
829
|
-
const publicKey = createPublicKey({
|
|
830
|
-
key: Buffer.from(spkiKey),
|
|
831
|
-
format: "der",
|
|
832
|
-
type: "spki"
|
|
833
|
-
});
|
|
834
|
-
const jwk = publicKey.export({ format: "jwk" });
|
|
835
|
-
return {
|
|
836
|
-
n: this.base64UrlToBigInt(jwk.n),
|
|
837
|
-
e: this.base64UrlToBigInt(jwk.e)
|
|
838
|
-
};
|
|
839
|
-
}
|
|
840
|
-
/**
|
|
841
|
-
* Convert base64 encoded data to a big int.
|
|
842
|
-
* @param bytes The bytes to convert.
|
|
843
|
-
* @returns The bigint representation of the bytes.
|
|
844
|
-
* @internal
|
|
845
|
-
*/
|
|
846
|
-
static base64UrlToBigInt(base64Url) {
|
|
847
|
-
if (Is.empty(base64Url)) {
|
|
848
|
-
return BigInt(0);
|
|
849
|
-
}
|
|
850
|
-
const bytes = Converter.base64UrlToBytes(base64Url);
|
|
851
|
-
const hexString = Array.from(bytes)
|
|
852
|
-
.map(byte => byte.toString(16).padStart(2, "0"))
|
|
853
|
-
.join("");
|
|
854
|
-
return BigInt(`0x${hexString}`);
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* Encrypt the data.
|
|
858
|
-
* @param data The data to encrypt.
|
|
859
|
-
* @returns The data encrypted.
|
|
860
|
-
*/
|
|
861
|
-
encrypt(data) {
|
|
862
|
-
Guards.uint8Array(RSA._CLASS_NAME, "data", data);
|
|
863
|
-
if (data.length === 0) {
|
|
864
|
-
return new Uint8Array(0);
|
|
865
|
-
}
|
|
866
|
-
const blocks = [];
|
|
867
|
-
// Split data into blocks of block size
|
|
868
|
-
for (let i = 0; i < data.length; i += this._blockSize) {
|
|
869
|
-
const block = data.slice(i, i + this._blockSize);
|
|
870
|
-
const encryptedBlock = publicEncrypt({
|
|
871
|
-
key: this._publicKey,
|
|
872
|
-
padding: constants.RSA_PKCS1_OAEP_PADDING
|
|
873
|
-
}, block);
|
|
874
|
-
blocks.push(encryptedBlock);
|
|
875
|
-
}
|
|
876
|
-
return Uint8ArrayHelper.concat(blocks);
|
|
877
|
-
}
|
|
878
|
-
/**
|
|
879
|
-
* Decrypt the data.
|
|
880
|
-
* @param data The data to decrypt.
|
|
881
|
-
* @returns The data decrypted.
|
|
882
|
-
* @throws GeneralError If no private key is provided.
|
|
883
|
-
*/
|
|
884
|
-
decrypt(data) {
|
|
885
|
-
Guards.uint8Array(RSA._CLASS_NAME, "data", data);
|
|
886
|
-
if (Is.empty(this._privateKey)) {
|
|
887
|
-
throw new GeneralError(RSA._CLASS_NAME, "noPrivateKey");
|
|
888
|
-
}
|
|
889
|
-
if (data.length === 0) {
|
|
890
|
-
return new Uint8Array(0);
|
|
891
|
-
}
|
|
892
|
-
const blocks = [];
|
|
893
|
-
// Split encrypted data into blocks of key size
|
|
894
|
-
for (let i = 0; i < data.length; i += this._keySize) {
|
|
895
|
-
const block = data.slice(i, i + this._keySize);
|
|
896
|
-
const decryptedBlock = privateDecrypt({
|
|
897
|
-
key: this._privateKey,
|
|
898
|
-
padding: constants.RSA_PKCS1_OAEP_PADDING
|
|
899
|
-
}, block);
|
|
900
|
-
blocks.push(decryptedBlock);
|
|
901
|
-
}
|
|
902
|
-
return Uint8ArrayHelper.concat(blocks);
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
|
|
906
695
|
// Copyright 2024 IOTA Stiftung.
|
|
907
696
|
// SPDX-License-Identifier: Apache-2.0.
|
|
908
697
|
/**
|
|
@@ -921,7 +710,7 @@ class X25519 {
|
|
|
921
710
|
*/
|
|
922
711
|
static convertPrivateKeyToX25519(ed25519PrivateKey) {
|
|
923
712
|
Guards.uint8Array(X25519._CLASS_NAME, "ed25519PrivateKey", ed25519PrivateKey);
|
|
924
|
-
return
|
|
713
|
+
return ed25519.utils.toMontgomerySecret(ed25519PrivateKey.slice(0, Ed25519.PRIVATE_KEY_SIZE));
|
|
925
714
|
}
|
|
926
715
|
/**
|
|
927
716
|
* Convert Ed25519 public key to X25519 public key.
|
|
@@ -931,7 +720,7 @@ class X25519 {
|
|
|
931
720
|
*/
|
|
932
721
|
static convertPublicKeyToX25519(ed25519PublicKey) {
|
|
933
722
|
Guards.uint8Array(X25519._CLASS_NAME, "ed25519PublicKey", ed25519PublicKey);
|
|
934
|
-
return
|
|
723
|
+
return ed25519.utils.toMontgomery(ed25519PublicKey);
|
|
935
724
|
}
|
|
936
725
|
}
|
|
937
726
|
|
|
@@ -2024,4 +1813,4 @@ class PasswordValidator {
|
|
|
2024
1813
|
}
|
|
2025
1814
|
}
|
|
2026
1815
|
|
|
2027
|
-
export { Bech32, Bip32Path, Bip39, Bip44, Blake2b, Blake3, ChaCha20Poly1305, Ed25519, HmacSha1, HmacSha256, HmacSha512, Hotp, KeyType, PasswordGenerator, PasswordValidator, Pbkdf2, PemHelper,
|
|
1816
|
+
export { Bech32, Bip32Path, Bip39, Bip44, Blake2b, Blake3, ChaCha20Poly1305, Ed25519, HmacSha1, HmacSha256, HmacSha512, Hotp, KeyType, PasswordGenerator, PasswordValidator, Pbkdf2, PemHelper, Secp256k1, Sha1, Sha256, Sha3, Sha512, Slip0010, Totp, X25519, Zip215 };
|
package/dist/types/index.d.ts
CHANGED
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# @twin.org/crypto - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.2-next.5](https://github.com/twinfoundation/framework/compare/crypto-v0.0.2-next.4...crypto-v0.0.2-next.5) (2025-08-19)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* use cause instead of inner for errors ([1f4acc4](https://github.com/twinfoundation/framework/commit/1f4acc4d7a6b71a134d9547da9bf40de1e1e49da))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/core bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
16
|
+
* @twin.org/nameof bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
17
|
+
* devDependencies
|
|
18
|
+
* @twin.org/nameof-transformer bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
19
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
20
|
+
|
|
21
|
+
## [0.0.2-next.4](https://github.com/twinfoundation/framework/compare/crypto-v0.0.2-next.3...crypto-v0.0.2-next.4) (2025-08-15)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
* additional RSA methods and async ([1fceee2](https://github.com/twinfoundation/framework/commit/1fceee2d1248a24a7620846025fcf906495c07f4))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Dependencies
|
|
30
|
+
|
|
31
|
+
* The following workspace dependencies were updated
|
|
32
|
+
* dependencies
|
|
33
|
+
* @twin.org/core bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
34
|
+
* @twin.org/nameof bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
35
|
+
* devDependencies
|
|
36
|
+
* @twin.org/nameof-transformer bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
37
|
+
* @twin.org/nameof-vitest-plugin bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
38
|
+
|
|
3
39
|
## [0.0.2-next.3](https://github.com/twinfoundation/framework/compare/crypto-v0.0.2-next.2...crypto-v0.0.2-next.3) (2025-08-06)
|
|
4
40
|
|
|
5
41
|
|
package/docs/reference/index.md
CHANGED
package/locales/en.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/crypto",
|
|
3
|
-
"version": "0.0.2-next.
|
|
3
|
+
"version": "0.0.2-next.5",
|
|
4
4
|
"description": "Contains helper methods and classes which implement cryptographic functions",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,13 +15,14 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@noble/ciphers": "1.3.0",
|
|
18
|
-
"@noble/curves": "1.9.
|
|
18
|
+
"@noble/curves": "1.9.7",
|
|
19
19
|
"@noble/hashes": "1.8.0",
|
|
20
20
|
"@scure/base": "1.2.6",
|
|
21
21
|
"@scure/bip32": "1.7.0",
|
|
22
22
|
"@scure/bip39": "1.6.0",
|
|
23
|
-
"@twin.org/core": "0.0.2-next.
|
|
24
|
-
"@twin.org/nameof": "0.0.2-next.
|
|
23
|
+
"@twin.org/core": "0.0.2-next.5",
|
|
24
|
+
"@twin.org/nameof": "0.0.2-next.5",
|
|
25
|
+
"crypto-browserify": "3.12.1",
|
|
25
26
|
"micro-key-producer": "0.7.6"
|
|
26
27
|
},
|
|
27
28
|
"main": "./dist/cjs/index.cjs",
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Implementation of the RSA cipher.
|
|
3
|
-
*/
|
|
4
|
-
export declare class RSA {
|
|
5
|
-
/**
|
|
6
|
-
* Create a new instance of RSA.
|
|
7
|
-
* @param publicKey The public key for encryption (DER format as Uint8Array).
|
|
8
|
-
* @param privateKey The private key for decryption (DER format as Uint8Array).
|
|
9
|
-
*/
|
|
10
|
-
constructor(publicKey: Uint8Array, privateKey?: Uint8Array);
|
|
11
|
-
/**
|
|
12
|
-
* Generate a new RSA key pair in PKCS8 format.
|
|
13
|
-
* @param modulusLength The key size in bits (default: 2048).
|
|
14
|
-
* @returns The public and private keys as Uint8Array.
|
|
15
|
-
*/
|
|
16
|
-
static generateKeyPair(modulusLength?: number): {
|
|
17
|
-
publicKey: Uint8Array;
|
|
18
|
-
privateKey: Uint8Array;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Convert a PKCS1 key to a PKCS8 key.
|
|
22
|
-
* @param pkcs1Key The PKCS1 key as Uint8Array.
|
|
23
|
-
* @returns The PKCS8 key as Uint8Array.
|
|
24
|
-
*/
|
|
25
|
-
static convertPkcs1ToPkcs8(pkcs1Key: Uint8Array): Uint8Array;
|
|
26
|
-
/**
|
|
27
|
-
* Break the private key down in to its components.
|
|
28
|
-
* @param pkcs8Key The PKCS8 key as Uint8Array.
|
|
29
|
-
* @returns The key components.
|
|
30
|
-
*/
|
|
31
|
-
static getPrivateKeyComponents(pkcs8Key: Uint8Array): {
|
|
32
|
-
n: bigint;
|
|
33
|
-
e: bigint;
|
|
34
|
-
d: bigint;
|
|
35
|
-
p: bigint;
|
|
36
|
-
q: bigint;
|
|
37
|
-
dp: bigint;
|
|
38
|
-
dq: bigint;
|
|
39
|
-
qi: bigint;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Break the public key down in to its components.
|
|
43
|
-
* @param spkiKey The SPKI key as Uint8Array.
|
|
44
|
-
* @returns The key components.
|
|
45
|
-
*/
|
|
46
|
-
static getPublicKeyComponents(spkiKey: Uint8Array): {
|
|
47
|
-
n: bigint;
|
|
48
|
-
e: bigint;
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Encrypt the data.
|
|
52
|
-
* @param data The data to encrypt.
|
|
53
|
-
* @returns The data encrypted.
|
|
54
|
-
*/
|
|
55
|
-
encrypt(data: Uint8Array): Uint8Array;
|
|
56
|
-
/**
|
|
57
|
-
* Decrypt the data.
|
|
58
|
-
* @param data The data to decrypt.
|
|
59
|
-
* @returns The data decrypted.
|
|
60
|
-
* @throws GeneralError If no private key is provided.
|
|
61
|
-
*/
|
|
62
|
-
decrypt(data: Uint8Array): Uint8Array;
|
|
63
|
-
}
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
# Class: RSA
|
|
2
|
-
|
|
3
|
-
Implementation of the RSA cipher.
|
|
4
|
-
|
|
5
|
-
## Constructors
|
|
6
|
-
|
|
7
|
-
### Constructor
|
|
8
|
-
|
|
9
|
-
> **new RSA**(`publicKey`, `privateKey?`): `RSA`
|
|
10
|
-
|
|
11
|
-
Create a new instance of RSA.
|
|
12
|
-
|
|
13
|
-
#### Parameters
|
|
14
|
-
|
|
15
|
-
##### publicKey
|
|
16
|
-
|
|
17
|
-
`Uint8Array`
|
|
18
|
-
|
|
19
|
-
The public key for encryption (DER format as Uint8Array).
|
|
20
|
-
|
|
21
|
-
##### privateKey?
|
|
22
|
-
|
|
23
|
-
`Uint8Array`\<`ArrayBufferLike`\>
|
|
24
|
-
|
|
25
|
-
The private key for decryption (DER format as Uint8Array).
|
|
26
|
-
|
|
27
|
-
#### Returns
|
|
28
|
-
|
|
29
|
-
`RSA`
|
|
30
|
-
|
|
31
|
-
## Methods
|
|
32
|
-
|
|
33
|
-
### generateKeyPair()
|
|
34
|
-
|
|
35
|
-
> `static` **generateKeyPair**(`modulusLength`): `object`
|
|
36
|
-
|
|
37
|
-
Generate a new RSA key pair in PKCS8 format.
|
|
38
|
-
|
|
39
|
-
#### Parameters
|
|
40
|
-
|
|
41
|
-
##### modulusLength
|
|
42
|
-
|
|
43
|
-
`number` = `2048`
|
|
44
|
-
|
|
45
|
-
The key size in bits (default: 2048).
|
|
46
|
-
|
|
47
|
-
#### Returns
|
|
48
|
-
|
|
49
|
-
`object`
|
|
50
|
-
|
|
51
|
-
The public and private keys as Uint8Array.
|
|
52
|
-
|
|
53
|
-
##### publicKey
|
|
54
|
-
|
|
55
|
-
> **publicKey**: `Uint8Array`
|
|
56
|
-
|
|
57
|
-
##### privateKey
|
|
58
|
-
|
|
59
|
-
> **privateKey**: `Uint8Array`
|
|
60
|
-
|
|
61
|
-
***
|
|
62
|
-
|
|
63
|
-
### convertPkcs1ToPkcs8()
|
|
64
|
-
|
|
65
|
-
> `static` **convertPkcs1ToPkcs8**(`pkcs1Key`): `Uint8Array`
|
|
66
|
-
|
|
67
|
-
Convert a PKCS1 key to a PKCS8 key.
|
|
68
|
-
|
|
69
|
-
#### Parameters
|
|
70
|
-
|
|
71
|
-
##### pkcs1Key
|
|
72
|
-
|
|
73
|
-
`Uint8Array`
|
|
74
|
-
|
|
75
|
-
The PKCS1 key as Uint8Array.
|
|
76
|
-
|
|
77
|
-
#### Returns
|
|
78
|
-
|
|
79
|
-
`Uint8Array`
|
|
80
|
-
|
|
81
|
-
The PKCS8 key as Uint8Array.
|
|
82
|
-
|
|
83
|
-
***
|
|
84
|
-
|
|
85
|
-
### getPrivateKeyComponents()
|
|
86
|
-
|
|
87
|
-
> `static` **getPrivateKeyComponents**(`pkcs8Key`): `object`
|
|
88
|
-
|
|
89
|
-
Break the private key down in to its components.
|
|
90
|
-
|
|
91
|
-
#### Parameters
|
|
92
|
-
|
|
93
|
-
##### pkcs8Key
|
|
94
|
-
|
|
95
|
-
`Uint8Array`
|
|
96
|
-
|
|
97
|
-
The PKCS8 key as Uint8Array.
|
|
98
|
-
|
|
99
|
-
#### Returns
|
|
100
|
-
|
|
101
|
-
`object`
|
|
102
|
-
|
|
103
|
-
The key components.
|
|
104
|
-
|
|
105
|
-
##### n
|
|
106
|
-
|
|
107
|
-
> **n**: `bigint`
|
|
108
|
-
|
|
109
|
-
##### e
|
|
110
|
-
|
|
111
|
-
> **e**: `bigint`
|
|
112
|
-
|
|
113
|
-
##### d
|
|
114
|
-
|
|
115
|
-
> **d**: `bigint`
|
|
116
|
-
|
|
117
|
-
##### p
|
|
118
|
-
|
|
119
|
-
> **p**: `bigint`
|
|
120
|
-
|
|
121
|
-
##### q
|
|
122
|
-
|
|
123
|
-
> **q**: `bigint`
|
|
124
|
-
|
|
125
|
-
##### dp
|
|
126
|
-
|
|
127
|
-
> **dp**: `bigint`
|
|
128
|
-
|
|
129
|
-
##### dq
|
|
130
|
-
|
|
131
|
-
> **dq**: `bigint`
|
|
132
|
-
|
|
133
|
-
##### qi
|
|
134
|
-
|
|
135
|
-
> **qi**: `bigint`
|
|
136
|
-
|
|
137
|
-
***
|
|
138
|
-
|
|
139
|
-
### getPublicKeyComponents()
|
|
140
|
-
|
|
141
|
-
> `static` **getPublicKeyComponents**(`spkiKey`): `object`
|
|
142
|
-
|
|
143
|
-
Break the public key down in to its components.
|
|
144
|
-
|
|
145
|
-
#### Parameters
|
|
146
|
-
|
|
147
|
-
##### spkiKey
|
|
148
|
-
|
|
149
|
-
`Uint8Array`
|
|
150
|
-
|
|
151
|
-
The SPKI key as Uint8Array.
|
|
152
|
-
|
|
153
|
-
#### Returns
|
|
154
|
-
|
|
155
|
-
`object`
|
|
156
|
-
|
|
157
|
-
The key components.
|
|
158
|
-
|
|
159
|
-
##### n
|
|
160
|
-
|
|
161
|
-
> **n**: `bigint`
|
|
162
|
-
|
|
163
|
-
##### e
|
|
164
|
-
|
|
165
|
-
> **e**: `bigint`
|
|
166
|
-
|
|
167
|
-
***
|
|
168
|
-
|
|
169
|
-
### encrypt()
|
|
170
|
-
|
|
171
|
-
> **encrypt**(`data`): `Uint8Array`
|
|
172
|
-
|
|
173
|
-
Encrypt the data.
|
|
174
|
-
|
|
175
|
-
#### Parameters
|
|
176
|
-
|
|
177
|
-
##### data
|
|
178
|
-
|
|
179
|
-
`Uint8Array`
|
|
180
|
-
|
|
181
|
-
The data to encrypt.
|
|
182
|
-
|
|
183
|
-
#### Returns
|
|
184
|
-
|
|
185
|
-
`Uint8Array`
|
|
186
|
-
|
|
187
|
-
The data encrypted.
|
|
188
|
-
|
|
189
|
-
***
|
|
190
|
-
|
|
191
|
-
### decrypt()
|
|
192
|
-
|
|
193
|
-
> **decrypt**(`data`): `Uint8Array`
|
|
194
|
-
|
|
195
|
-
Decrypt the data.
|
|
196
|
-
|
|
197
|
-
#### Parameters
|
|
198
|
-
|
|
199
|
-
##### data
|
|
200
|
-
|
|
201
|
-
`Uint8Array`
|
|
202
|
-
|
|
203
|
-
The data to decrypt.
|
|
204
|
-
|
|
205
|
-
#### Returns
|
|
206
|
-
|
|
207
|
-
`Uint8Array`
|
|
208
|
-
|
|
209
|
-
The data decrypted.
|
|
210
|
-
|
|
211
|
-
#### Throws
|
|
212
|
-
|
|
213
|
-
GeneralError If no private key is provided.
|