cas-typescript-sdk 1.0.28 → 1.0.29
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/index.d.ts +0 -2
- package/index.node +0 -0
- package/lib/asymmetric/RSAWrapper.d.ts +0 -8
- package/lib/asymmetric/RSAWrapper.js +0 -24
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/package.json +2 -2
- package/src/asymmetric/cas_rsa.rs +0 -10
- package/src-ts/asymmetric/RSAWrapper.ts +1 -28
- package/src-ts/index.ts +0 -1
- package/test-ts/asymmetric.test.spec.ts +0 -13
- package/lib/hybrid/hybrid-encryption-wrapper.d.ts +0 -21
- package/lib/hybrid/hybrid-encryption-wrapper.js +0 -42
- package/lib/hybrid/index.d.ts +0 -4
- package/lib/hybrid/index.js +0 -9
- package/lib/hybrid/types/aes-rsa-hybird-encrypt-result.d.ts +0 -7
- package/lib/hybrid/types/aes-rsa-hybird-encrypt-result.js +0 -16
- package/lib/hybrid/types/aes-rsa-hybrid-initializer.d.ts +0 -13
- package/lib/hybrid/types/aes-rsa-hybrid-initializer.js +0 -30
- package/src-ts/hybrid/hybrid-encryption-wrapper.ts +0 -76
- package/src-ts/hybrid/index.ts +0 -9
- package/src-ts/hybrid/types/aes-rsa-hybird-encrypt-result.ts +0 -13
- package/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts +0 -29
- package/test-ts/hybrid.test.spec.ts +0 -33
package/index.d.ts
CHANGED
|
@@ -31,8 +31,6 @@ export declare function aes256Decrypt(aesKey: Array<number>, nonce: Array<number
|
|
|
31
31
|
export declare function aes256KeyFromX25519SharedSecret(sharedSecret: Array<number>): CASAesKeyFromX25519SharedSecret
|
|
32
32
|
export declare function aes128KeyFromX25519SharedSecret(sharedSecret: Array<number>): CASAesKeyFromX25519SharedSecret
|
|
33
33
|
export declare function generateRsaKeys(keySize: number): CasrsaKeyPairResult
|
|
34
|
-
export declare function encryptPlaintextRsa(publicKey: string, plaintext: Array<number>): Array<number>
|
|
35
|
-
export declare function decryptCiphertextRsa(privateKey: string, ciphertext: Array<number>): Array<number>
|
|
36
34
|
export declare function signRsa(privateKey: string, hash: Array<number>): Array<number>
|
|
37
35
|
export declare function verifyRsa(publicKey: string, hash: Array<number>, signature: Array<number>): boolean
|
|
38
36
|
export declare function sha512RsaDigitalSignature(rsaKeySize: number, dataToSign: Array<number>): CASRSADigitalSignatureResult
|
package/index.node
CHANGED
|
Binary file
|
|
@@ -12,14 +12,6 @@ export declare class RSAWrapper {
|
|
|
12
12
|
* @param plaintext
|
|
13
13
|
* @returns Array<number>
|
|
14
14
|
*/
|
|
15
|
-
encrypt(publicKey: string, plaintext: Array<number>): Array<number>;
|
|
16
|
-
/**
|
|
17
|
-
* Decrypts a ciphertext with an RSA private key.
|
|
18
|
-
* @param privateKey
|
|
19
|
-
* @param ciphertext
|
|
20
|
-
* @returns Array<number>
|
|
21
|
-
*/
|
|
22
|
-
decrypt(privateKey: string, ciphertext: Array<number>): Array<number>;
|
|
23
15
|
/**
|
|
24
16
|
* Signs a byte array with an RSA private key for verification.
|
|
25
17
|
* @param privateKey
|
|
@@ -20,30 +20,6 @@ class RSAWrapper {
|
|
|
20
20
|
* @param plaintext
|
|
21
21
|
* @returns Array<number>
|
|
22
22
|
*/
|
|
23
|
-
encrypt(publicKey, plaintext) {
|
|
24
|
-
if (!publicKey) {
|
|
25
|
-
throw new Error("You must provide a public key to encrypt with RSA");
|
|
26
|
-
}
|
|
27
|
-
if (!plaintext || plaintext.length === 0) {
|
|
28
|
-
throw new Error("You must provide an array of plaintext bytes to encrypt with RSA");
|
|
29
|
-
}
|
|
30
|
-
return (0, index_1.encryptPlaintextRsa)(publicKey, plaintext);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Decrypts a ciphertext with an RSA private key.
|
|
34
|
-
* @param privateKey
|
|
35
|
-
* @param ciphertext
|
|
36
|
-
* @returns Array<number>
|
|
37
|
-
*/
|
|
38
|
-
decrypt(privateKey, ciphertext) {
|
|
39
|
-
if (!privateKey) {
|
|
40
|
-
throw new Error("You must provide a private key to encrypt with RSA");
|
|
41
|
-
}
|
|
42
|
-
if (!ciphertext || ciphertext.length === 0) {
|
|
43
|
-
throw new Error("You must provide an array of ciphertext bytes to encrypt with RSA");
|
|
44
|
-
}
|
|
45
|
-
return (0, index_1.decryptCiphertextRsa)(privateKey, ciphertext);
|
|
46
|
-
}
|
|
47
23
|
/**
|
|
48
24
|
* Signs a byte array with an RSA private key for verification.
|
|
49
25
|
* @param privateKey
|
package/lib/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export * from "./hashers/index";
|
|
|
3
3
|
export * from "./key_exchange/index";
|
|
4
4
|
export * from "./symmetric/index";
|
|
5
5
|
export * from "./asymmetric/index";
|
|
6
|
-
export * from "./hybrid/index";
|
|
7
6
|
export * from "./digital-signature";
|
|
8
7
|
export * from "./sponges/index";
|
|
9
8
|
export * from "./message/index";
|
package/lib/index.js
CHANGED
|
@@ -19,7 +19,6 @@ __exportStar(require("./hashers/index"), exports);
|
|
|
19
19
|
__exportStar(require("./key_exchange/index"), exports);
|
|
20
20
|
__exportStar(require("./symmetric/index"), exports);
|
|
21
21
|
__exportStar(require("./asymmetric/index"), exports);
|
|
22
|
-
__exportStar(require("./hybrid/index"), exports);
|
|
23
22
|
__exportStar(require("./digital-signature"), exports);
|
|
24
23
|
__exportStar(require("./sponges/index"), exports);
|
|
25
24
|
__exportStar(require("./message/index"), exports);
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cas-typescript-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.29",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "cargo test && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
|
|
8
|
+
"test": "cargo test --release && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
|
|
9
9
|
"node:test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
|
|
10
10
|
"rust:test": "cargo test",
|
|
11
11
|
"build": "npm run build:rust && rimraf lib && tsc",
|
|
@@ -21,16 +21,6 @@ pub fn generate_rsa_keys(key_size: u32) -> CASRSAKeyPairResult {
|
|
|
21
21
|
return CASRSA::generate_rsa_keys(key_size as usize).into();
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
#[napi]
|
|
25
|
-
pub fn encrypt_plaintext_rsa(public_key: String, plaintext: Vec<u8>) -> Vec<u8> {
|
|
26
|
-
return CASRSA::encrypt_plaintext(public_key, plaintext);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
#[napi]
|
|
30
|
-
pub fn decrypt_ciphertext_rsa(private_key: String, ciphertext: Vec<u8>) -> Vec<u8> {
|
|
31
|
-
return CASRSA::decrypt_ciphertext(private_key, ciphertext);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
24
|
#[napi]
|
|
35
25
|
pub fn sign_rsa(private_key: String, hash: Vec<u8>) -> Vec<u8> {
|
|
36
26
|
return CASRSA::sign(private_key, hash);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CASRSAKeyPairResult,
|
|
1
|
+
import { CASRSAKeyPairResult, generateRsaKeys, signRsa, verifyRsa } from "../../index";
|
|
2
2
|
|
|
3
3
|
export class RSAWrapper {
|
|
4
4
|
|
|
@@ -21,33 +21,6 @@ export class RSAWrapper {
|
|
|
21
21
|
* @returns Array<number>
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
public encrypt(publicKey: string, plaintext: Array<number>): Array<number> {
|
|
25
|
-
if (!publicKey) {
|
|
26
|
-
throw new Error("You must provide a public key to encrypt with RSA");
|
|
27
|
-
}
|
|
28
|
-
if (!plaintext || plaintext.length === 0) {
|
|
29
|
-
throw new Error("You must provide an array of plaintext bytes to encrypt with RSA");
|
|
30
|
-
}
|
|
31
|
-
return encryptPlaintextRsa(publicKey, plaintext);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Decrypts a ciphertext with an RSA private key.
|
|
36
|
-
* @param privateKey
|
|
37
|
-
* @param ciphertext
|
|
38
|
-
* @returns Array<number>
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
public decrypt(privateKey: string, ciphertext: Array<number>): Array<number> {
|
|
42
|
-
if (!privateKey) {
|
|
43
|
-
throw new Error("You must provide a private key to encrypt with RSA");
|
|
44
|
-
}
|
|
45
|
-
if (!ciphertext || ciphertext.length === 0) {
|
|
46
|
-
throw new Error("You must provide an array of ciphertext bytes to encrypt with RSA");
|
|
47
|
-
}
|
|
48
|
-
return decryptCiphertextRsa(privateKey, ciphertext);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
24
|
/**
|
|
52
25
|
* Signs a byte array with an RSA private key for verification.
|
|
53
26
|
* @param privateKey
|
package/src-ts/index.ts
CHANGED
|
@@ -3,7 +3,6 @@ export * from "./hashers/index";
|
|
|
3
3
|
export * from "./key_exchange/index";
|
|
4
4
|
export * from "./symmetric/index";
|
|
5
5
|
export * from "./asymmetric/index";
|
|
6
|
-
export * from "./hybrid/index";
|
|
7
6
|
export * from "./digital-signature";
|
|
8
7
|
export * from "./sponges/index";
|
|
9
8
|
export * from "./message/index";
|
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
import { assert } from "chai";
|
|
2
2
|
import { CASRSAKeyPairResult, RSAWrapper } from "..";
|
|
3
|
-
import { areEqual } from "./helpers/array";
|
|
4
3
|
|
|
5
4
|
describe("Asymmetric Tests", () => {
|
|
6
|
-
it("RSA 4096 encrypt and decrypt equals", () => {
|
|
7
|
-
const rsaWrapper: RSAWrapper = new RSAWrapper();
|
|
8
|
-
const keys: CASRSAKeyPairResult = rsaWrapper.generateKeys(4096);
|
|
9
|
-
const tohashed: string = "This is my array to encrypt";
|
|
10
|
-
const encoder = new TextEncoder();
|
|
11
|
-
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
|
|
12
|
-
const ciphertext = rsaWrapper.encrypt(keys.publicKey, tohashBytes);
|
|
13
|
-
const plaintext = rsaWrapper.decrypt(keys.privateKey, ciphertext);
|
|
14
|
-
let result = areEqual(tohashBytes, plaintext);
|
|
15
|
-
assert.isTrue(result);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
5
|
it("RSA 2048 Sign and Verify", () => {
|
|
19
6
|
const rsaWrapper = new RSAWrapper();
|
|
20
7
|
const keys: CASRSAKeyPairResult = rsaWrapper.generateKeys(2048);
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { AesRsaHybridEncryptResult } from "./types/aes-rsa-hybird-encrypt-result";
|
|
2
|
-
import { AESRSAHybridInitializer } from "./types/aes-rsa-hybrid-initializer";
|
|
3
|
-
export declare class HybridEncryptionWrapper {
|
|
4
|
-
private aesWrapper;
|
|
5
|
-
private rsaWrapper;
|
|
6
|
-
constructor();
|
|
7
|
-
/**
|
|
8
|
-
* Encrypts data with RSA/AES hybrid encryption. The data is encrypted with AES-GCM and the AES key is encrypted with the RSA public key.
|
|
9
|
-
* @param dataToEncrypt
|
|
10
|
-
* @param initalizer
|
|
11
|
-
* @returns AesRsaHybridEncryptResult
|
|
12
|
-
*/
|
|
13
|
-
encrypt(dataToEncrypt: Array<number>, initalizer: AESRSAHybridInitializer): AesRsaHybridEncryptResult;
|
|
14
|
-
/**
|
|
15
|
-
* Decrypts data with RSA/AES hybrid encryption. The RSA private key decrypts the AES key and then the data is decrypted with AES-GCM.
|
|
16
|
-
* @param dataToEncrypt
|
|
17
|
-
* @param initalizer
|
|
18
|
-
* @returns AesRsaHybridEncryptResult
|
|
19
|
-
*/
|
|
20
|
-
decrypt(privateKey: string, encryptResult: AesRsaHybridEncryptResult): Array<number>;
|
|
21
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HybridEncryptionWrapper = void 0;
|
|
4
|
-
const asymmetric_1 = require("../asymmetric");
|
|
5
|
-
const symmetric_1 = require("../symmetric");
|
|
6
|
-
const aes_rsa_hybird_encrypt_result_1 = require("./types/aes-rsa-hybird-encrypt-result");
|
|
7
|
-
class HybridEncryptionWrapper {
|
|
8
|
-
aesWrapper;
|
|
9
|
-
rsaWrapper;
|
|
10
|
-
constructor() {
|
|
11
|
-
this.aesWrapper = new symmetric_1.AESWrapper();
|
|
12
|
-
this.rsaWrapper = new asymmetric_1.RSAWrapper();
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Encrypts data with RSA/AES hybrid encryption. The data is encrypted with AES-GCM and the AES key is encrypted with the RSA public key.
|
|
16
|
-
* @param dataToEncrypt
|
|
17
|
-
* @param initalizer
|
|
18
|
-
* @returns AesRsaHybridEncryptResult
|
|
19
|
-
*/
|
|
20
|
-
encrypt(dataToEncrypt, initalizer) {
|
|
21
|
-
let encryptedData = (initalizer.aesType === 128)
|
|
22
|
-
? this.aesWrapper.aes128Encrypt(initalizer.aesKey, initalizer.aesNonce, dataToEncrypt)
|
|
23
|
-
: this.aesWrapper.aes256Encrypt(initalizer.aesKey, initalizer.aesNonce, dataToEncrypt);
|
|
24
|
-
let encryptedAesKey = this.rsaWrapper.encrypt(initalizer.rsaKeyPair.publicKey, initalizer.aesKey);
|
|
25
|
-
let result = new aes_rsa_hybird_encrypt_result_1.AesRsaHybridEncryptResult(encryptedData, encryptedAesKey, initalizer.aesType, initalizer.aesNonce);
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Decrypts data with RSA/AES hybrid encryption. The RSA private key decrypts the AES key and then the data is decrypted with AES-GCM.
|
|
30
|
-
* @param dataToEncrypt
|
|
31
|
-
* @param initalizer
|
|
32
|
-
* @returns AesRsaHybridEncryptResult
|
|
33
|
-
*/
|
|
34
|
-
decrypt(privateKey, encryptResult) {
|
|
35
|
-
let plaintextAesKey = this.rsaWrapper.decrypt(privateKey, encryptResult.encryptedAesKey);
|
|
36
|
-
let plaintext = (encryptResult.aesType === 128)
|
|
37
|
-
? this.aesWrapper.aes128Decrypt(plaintextAesKey, encryptResult.aesNonce, encryptResult.ciphertext)
|
|
38
|
-
: this.aesWrapper.aes256Decrypt(plaintextAesKey, encryptResult.aesNonce, encryptResult.ciphertext);
|
|
39
|
-
return plaintext;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
exports.HybridEncryptionWrapper = HybridEncryptionWrapper;
|
package/lib/hybrid/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { HybridEncryptionWrapper } from "./hybrid-encryption-wrapper";
|
|
2
|
-
import { AesRsaHybridEncryptResult } from "./types/aes-rsa-hybird-encrypt-result";
|
|
3
|
-
import { AESRSAHybridInitializer } from "./types/aes-rsa-hybrid-initializer";
|
|
4
|
-
export { AesRsaHybridEncryptResult, AESRSAHybridInitializer, HybridEncryptionWrapper, };
|
package/lib/hybrid/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HybridEncryptionWrapper = exports.AESRSAHybridInitializer = exports.AesRsaHybridEncryptResult = void 0;
|
|
4
|
-
const hybrid_encryption_wrapper_1 = require("./hybrid-encryption-wrapper");
|
|
5
|
-
Object.defineProperty(exports, "HybridEncryptionWrapper", { enumerable: true, get: function () { return hybrid_encryption_wrapper_1.HybridEncryptionWrapper; } });
|
|
6
|
-
const aes_rsa_hybird_encrypt_result_1 = require("./types/aes-rsa-hybird-encrypt-result");
|
|
7
|
-
Object.defineProperty(exports, "AesRsaHybridEncryptResult", { enumerable: true, get: function () { return aes_rsa_hybird_encrypt_result_1.AesRsaHybridEncryptResult; } });
|
|
8
|
-
const aes_rsa_hybrid_initializer_1 = require("./types/aes-rsa-hybrid-initializer");
|
|
9
|
-
Object.defineProperty(exports, "AESRSAHybridInitializer", { enumerable: true, get: function () { return aes_rsa_hybrid_initializer_1.AESRSAHybridInitializer; } });
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export declare class AesRsaHybridEncryptResult {
|
|
2
|
-
ciphertext: Array<number>;
|
|
3
|
-
encryptedAesKey: Array<number>;
|
|
4
|
-
aesType: number;
|
|
5
|
-
aesNonce: Array<number>;
|
|
6
|
-
constructor(cipherText: Array<number>, encryptAesKey: Array<number>, aesType: number, aesNonce: Array<number>);
|
|
7
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AesRsaHybridEncryptResult = void 0;
|
|
4
|
-
class AesRsaHybridEncryptResult {
|
|
5
|
-
ciphertext;
|
|
6
|
-
encryptedAesKey;
|
|
7
|
-
aesType;
|
|
8
|
-
aesNonce;
|
|
9
|
-
constructor(cipherText, encryptAesKey, aesType, aesNonce) {
|
|
10
|
-
this.ciphertext = cipherText;
|
|
11
|
-
this.encryptedAesKey = encryptAesKey;
|
|
12
|
-
this.aesType = aesType;
|
|
13
|
-
this.aesNonce = aesNonce;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.AesRsaHybridEncryptResult = AesRsaHybridEncryptResult;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { CASRSAKeyPairResult } from "../../..";
|
|
2
|
-
export declare class AESRSAHybridInitializer {
|
|
3
|
-
aesType: number;
|
|
4
|
-
aesKey: Array<number>;
|
|
5
|
-
aesNonce: Array<number>;
|
|
6
|
-
rsaKeyPair: CASRSAKeyPairResult;
|
|
7
|
-
/**
|
|
8
|
-
* Constructs an initalizer to use with Hybrid Encryption wrapper. Generates your RSA key pair, AES nonce, and AES key based on the parameters passed in.
|
|
9
|
-
* @param aesType
|
|
10
|
-
* @param rsaSize
|
|
11
|
-
*/
|
|
12
|
-
constructor(aesType: number, rsaSize: number);
|
|
13
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AESRSAHybridInitializer = void 0;
|
|
4
|
-
const asymmetric_1 = require("../../asymmetric");
|
|
5
|
-
const symmetric_1 = require("../../symmetric");
|
|
6
|
-
class AESRSAHybridInitializer {
|
|
7
|
-
aesType;
|
|
8
|
-
aesKey;
|
|
9
|
-
aesNonce;
|
|
10
|
-
rsaKeyPair;
|
|
11
|
-
/**
|
|
12
|
-
* Constructs an initalizer to use with Hybrid Encryption wrapper. Generates your RSA key pair, AES nonce, and AES key based on the parameters passed in.
|
|
13
|
-
* @param aesType
|
|
14
|
-
* @param rsaSize
|
|
15
|
-
*/
|
|
16
|
-
constructor(aesType, rsaSize) {
|
|
17
|
-
if (aesType !== 128 && aesType !== 256) {
|
|
18
|
-
throw new Error("Need an appropriate AES size to generate a hybrid initalizer");
|
|
19
|
-
}
|
|
20
|
-
this.aesType = aesType;
|
|
21
|
-
let aesWrapper = new symmetric_1.AESWrapper();
|
|
22
|
-
this.aesKey = (aesType === 128) ? aesWrapper.aes128Key() : aesWrapper.aes256Key();
|
|
23
|
-
this.aesNonce = aesWrapper.generateAESNonce();
|
|
24
|
-
if (rsaSize !== 1028 && rsaSize !== 2048 && rsaSize !== 4096) {
|
|
25
|
-
throw new Error("You must provide an appropriate RSA Key pair size to generate a hybrid initalizer");
|
|
26
|
-
}
|
|
27
|
-
this.rsaKeyPair = new asymmetric_1.RSAWrapper().generateKeys(rsaSize);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.AESRSAHybridInitializer = AESRSAHybridInitializer;
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { RSAWrapper } from "../asymmetric";
|
|
2
|
-
import { AESWrapper } from "../symmetric";
|
|
3
|
-
import { AesRsaHybridEncryptResult } from "./types/aes-rsa-hybird-encrypt-result";
|
|
4
|
-
import { AESRSAHybridInitializer } from "./types/aes-rsa-hybrid-initializer";
|
|
5
|
-
|
|
6
|
-
export class HybridEncryptionWrapper {
|
|
7
|
-
private aesWrapper: AESWrapper;
|
|
8
|
-
private rsaWrapper: RSAWrapper;
|
|
9
|
-
|
|
10
|
-
constructor() {
|
|
11
|
-
this.aesWrapper = new AESWrapper();
|
|
12
|
-
this.rsaWrapper = new RSAWrapper();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Encrypts data with RSA/AES hybrid encryption. The data is encrypted with AES-GCM and the AES key is encrypted with the RSA public key.
|
|
17
|
-
* @param dataToEncrypt
|
|
18
|
-
* @param initalizer
|
|
19
|
-
* @returns AesRsaHybridEncryptResult
|
|
20
|
-
*/
|
|
21
|
-
public encrypt(
|
|
22
|
-
dataToEncrypt: Array<number>,
|
|
23
|
-
initalizer: AESRSAHybridInitializer,
|
|
24
|
-
): AesRsaHybridEncryptResult {
|
|
25
|
-
let encryptedData: Array<number> = (initalizer.aesType === 128)
|
|
26
|
-
? this.aesWrapper.aes128Encrypt(
|
|
27
|
-
initalizer.aesKey,
|
|
28
|
-
initalizer.aesNonce,
|
|
29
|
-
dataToEncrypt,
|
|
30
|
-
)
|
|
31
|
-
: this.aesWrapper.aes256Encrypt(
|
|
32
|
-
initalizer.aesKey,
|
|
33
|
-
initalizer.aesNonce,
|
|
34
|
-
dataToEncrypt,
|
|
35
|
-
);
|
|
36
|
-
let encryptedAesKey: Array<number> = this.rsaWrapper.encrypt(
|
|
37
|
-
initalizer.rsaKeyPair.publicKey,
|
|
38
|
-
initalizer.aesKey,
|
|
39
|
-
);
|
|
40
|
-
let result: AesRsaHybridEncryptResult = new AesRsaHybridEncryptResult(
|
|
41
|
-
encryptedData,
|
|
42
|
-
encryptedAesKey,
|
|
43
|
-
initalizer.aesType,
|
|
44
|
-
initalizer.aesNonce,
|
|
45
|
-
);
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Decrypts data with RSA/AES hybrid encryption. The RSA private key decrypts the AES key and then the data is decrypted with AES-GCM.
|
|
51
|
-
* @param dataToEncrypt
|
|
52
|
-
* @param initalizer
|
|
53
|
-
* @returns AesRsaHybridEncryptResult
|
|
54
|
-
*/
|
|
55
|
-
public decrypt(
|
|
56
|
-
privateKey: string,
|
|
57
|
-
encryptResult: AesRsaHybridEncryptResult,
|
|
58
|
-
): Array<number> {
|
|
59
|
-
let plaintextAesKey = this.rsaWrapper.decrypt(
|
|
60
|
-
privateKey,
|
|
61
|
-
encryptResult.encryptedAesKey,
|
|
62
|
-
);
|
|
63
|
-
let plaintext = (encryptResult.aesType === 128)
|
|
64
|
-
? this.aesWrapper.aes128Decrypt(
|
|
65
|
-
plaintextAesKey,
|
|
66
|
-
encryptResult.aesNonce,
|
|
67
|
-
encryptResult.ciphertext,
|
|
68
|
-
)
|
|
69
|
-
: this.aesWrapper.aes256Decrypt(
|
|
70
|
-
plaintextAesKey,
|
|
71
|
-
encryptResult.aesNonce,
|
|
72
|
-
encryptResult.ciphertext,
|
|
73
|
-
);
|
|
74
|
-
return plaintext;
|
|
75
|
-
}
|
|
76
|
-
}
|
package/src-ts/hybrid/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { HybridEncryptionWrapper } from "./hybrid-encryption-wrapper";
|
|
2
|
-
import { AesRsaHybridEncryptResult } from "./types/aes-rsa-hybird-encrypt-result";
|
|
3
|
-
import { AESRSAHybridInitializer } from "./types/aes-rsa-hybrid-initializer";
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
AesRsaHybridEncryptResult,
|
|
7
|
-
AESRSAHybridInitializer,
|
|
8
|
-
HybridEncryptionWrapper,
|
|
9
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export class AesRsaHybridEncryptResult {
|
|
2
|
-
ciphertext: Array<number>;
|
|
3
|
-
encryptedAesKey: Array<number>;
|
|
4
|
-
aesType: number;
|
|
5
|
-
aesNonce: Array<number>;
|
|
6
|
-
|
|
7
|
-
constructor(cipherText: Array<number>, encryptAesKey: Array<number>, aesType: number, aesNonce: Array<number>) {
|
|
8
|
-
this.ciphertext = cipherText;
|
|
9
|
-
this.encryptedAesKey = encryptAesKey;
|
|
10
|
-
this.aesType = aesType;
|
|
11
|
-
this.aesNonce = aesNonce;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { CASRSAKeyPairResult } from "../../..";
|
|
2
|
-
import { RSAWrapper } from "../../asymmetric";
|
|
3
|
-
import { AESWrapper } from "../../symmetric";
|
|
4
|
-
|
|
5
|
-
export class AESRSAHybridInitializer {
|
|
6
|
-
public aesType: number;
|
|
7
|
-
public aesKey: Array<number>;
|
|
8
|
-
public aesNonce: Array<number>;
|
|
9
|
-
public rsaKeyPair: CASRSAKeyPairResult;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Constructs an initalizer to use with Hybrid Encryption wrapper. Generates your RSA key pair, AES nonce, and AES key based on the parameters passed in.
|
|
13
|
-
* @param aesType
|
|
14
|
-
* @param rsaSize
|
|
15
|
-
*/
|
|
16
|
-
constructor(aesType: number, rsaSize: number) {
|
|
17
|
-
if (aesType !== 128 && aesType !== 256) {
|
|
18
|
-
throw new Error("Need an appropriate AES size to generate a hybrid initalizer");
|
|
19
|
-
}
|
|
20
|
-
this.aesType = aesType;
|
|
21
|
-
let aesWrapper = new AESWrapper();
|
|
22
|
-
this.aesKey = (aesType === 128) ? aesWrapper.aes128Key() : aesWrapper.aes256Key();
|
|
23
|
-
this.aesNonce = aesWrapper.generateAESNonce();
|
|
24
|
-
if (rsaSize !== 1028 && rsaSize !== 2048 && rsaSize !== 4096) {
|
|
25
|
-
throw new Error("You must provide an appropriate RSA Key pair size to generate a hybrid initalizer");
|
|
26
|
-
}
|
|
27
|
-
this.rsaKeyPair = new RSAWrapper().generateKeys(rsaSize);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { assert } from "chai";
|
|
2
|
-
import {
|
|
3
|
-
AESRSAHybridInitializer,
|
|
4
|
-
AesRsaHybridEncryptResult,
|
|
5
|
-
HybridEncryptionWrapper,
|
|
6
|
-
} from "../src-ts/hybrid/index";
|
|
7
|
-
import { areEqual } from "./helpers/array";
|
|
8
|
-
|
|
9
|
-
describe("Hybrid Encryption Tests", () => {
|
|
10
|
-
it("RSA 4096 AES 128 encrypt and decrypt equals", () => {
|
|
11
|
-
const hybridWrapper = new HybridEncryptionWrapper();
|
|
12
|
-
let initalizer = new AESRSAHybridInitializer(128, 4096);
|
|
13
|
-
const tohashed: string = "This is my encrypt text for rsa hybrid";
|
|
14
|
-
const encoder = new TextEncoder();
|
|
15
|
-
const toEncrypt: Array<number> = Array.from(encoder.encode(tohashed));
|
|
16
|
-
let result: AesRsaHybridEncryptResult = hybridWrapper.encrypt(toEncrypt, initalizer);
|
|
17
|
-
let plaintext: Array<number> = hybridWrapper.decrypt(initalizer.rsaKeyPair.privateKey, result);
|
|
18
|
-
let result2 = areEqual(toEncrypt, plaintext);
|
|
19
|
-
assert.isTrue(result2);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("RSA 2048 AES 256 encrypt and decrypt equals", () => {
|
|
23
|
-
const hybridWrapper = new HybridEncryptionWrapper();
|
|
24
|
-
let initalizer = new AESRSAHybridInitializer(256, 2048);
|
|
25
|
-
const tohashed: string = "This is my encrypt text for rsa hybrid";
|
|
26
|
-
const encoder = new TextEncoder();
|
|
27
|
-
const toEncrypt: Array<number> = Array.from(encoder.encode(tohashed));
|
|
28
|
-
let result: AesRsaHybridEncryptResult = hybridWrapper.encrypt(toEncrypt, initalizer);
|
|
29
|
-
let plaintext: Array<number> = hybridWrapper.decrypt(initalizer.rsaKeyPair.privateKey, result);
|
|
30
|
-
let result2 = areEqual(toEncrypt, plaintext);
|
|
31
|
-
assert.isTrue(result2);
|
|
32
|
-
});
|
|
33
|
-
});
|