Skip to content

Commit dd04911

Browse files
authored
Merge pull request #28 from Cryptographic-API-Services/pre-release
Pre release
2 parents 140bebc + 515a793 commit dd04911

18 files changed

+287
-27
lines changed

index.node

0 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{
33
"name": "cas-typescript-sdk",
4-
"version": "1.0.22",
4+
"version": "1.0.23",
55
"description": "",
66
"main": "lib/index.js",
77
"types": "lib/index.d.ts",

src-ts/asymmetric/RSAWrapper.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import { decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys, RsaKeyPairResult, signRsa, verifyRsa } from "../../index";
22

33
export class RSAWrapper {
4+
5+
/**
6+
* Generates an RSA key pair based of parameter sent in 1024, 2048, and 4096 are supported.
7+
* @param keySize
8+
* @returns RsaKeyPairResult
9+
*/
410
public generateKeys(keySize: number): RsaKeyPairResult {
511
if (keySize !== 1024 && keySize !== 2048 && keySize !== 4096) {
612
throw new Error("You must provide an appropriate key size to generate RSA keys");
713
}
814
return generateRsaKeys(keySize);
915
}
1016

17+
/**
18+
* Encrypts a plaintext byte array with a RSA public key
19+
* @param publicKey
20+
* @param plaintext
21+
* @returns Array<number>
22+
*/
23+
1124
public encrypt(publicKey: string, plaintext: Array<number>): Array<number> {
1225
if (!publicKey) {
1326
throw new Error("You must provide a public key to encrypt with RSA");
@@ -18,6 +31,13 @@ export class RSAWrapper {
1831
return encryptPlaintextRsa(publicKey, plaintext);
1932
}
2033

34+
/**
35+
* Decrypts a ciphertext with an RSA private key.
36+
* @param privateKey
37+
* @param ciphertext
38+
* @returns Array<number>
39+
*/
40+
2141
public decrypt(privateKey: string, ciphertext: Array<number>): Array<number> {
2242
if (!privateKey) {
2343
throw new Error("You must provide a private key to encrypt with RSA");
@@ -28,16 +48,29 @@ export class RSAWrapper {
2848
return decryptCiphertextRsa(privateKey, ciphertext);
2949
}
3050

31-
public sign(privateKey: string, hash: Array<number>): Array<number> {
51+
/**
52+
* Signs a byte array with an RSA private key for verification.
53+
* @param privateKey
54+
* @param hash
55+
* @returns Array<number>
56+
*/
57+
public sign(privateKey: string, dataToSign: Array<number>): Array<number> {
3258
if (!privateKey) {
3359
throw new Error("You must provide a private key to sign with RSA");
3460
}
35-
if (!hash || hash.length === 0) {
61+
if (!dataToSign || dataToSign.length === 0) {
3662
throw new Error("You must provide an allocated hash to sign with RSA");
3763
}
38-
return signRsa(privateKey, hash);
64+
return signRsa(privateKey, dataToSign);
3965
}
4066

67+
/**
68+
* Verifies signed data by the corresponding private key with an RSA public key.
69+
* @param publicKey
70+
* @param hash
71+
* @param signature
72+
* @returns boolean
73+
*/
4174
public verify(publicKey: string, hash: Array<number>, signature: Array<number>): boolean {
4275
if (!publicKey) {
4376
throw new Error("You must provide a public key to verify with RSA");

src-ts/digital-signature/digital-siganture-sha-512.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ import { IDigitalSignature } from "./digital-signature-base";
33

44
export class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
55

6+
/**
7+
* Creates an ED25519 siganture from an array of bytes with SHA3-512.
8+
* @param dataToSign
9+
* @returns SHAED25519DalekDigitalSignatureResult
10+
*/
611
createED25519(dataToSign: number[]): SHAED25519DalekDigitalSignatureResult {
712
if (dataToSign?.length === 0) {
813
throw new Error("Must provide allocated data to sign");
914
}
1015
return sha512Ed25519DigitalSignature(dataToSign);
1116
}
12-
17+
18+
/**
19+
* Verifies an ED25519 signature with the public key generated from running createED25519() with SHA3-512
20+
* @param publicKey
21+
* @param dataToVerify
22+
* @param signature
23+
* @returns boolean
24+
*/
1325
verifyED25519(publicKey: number[], dataToVerify: number[], signature: number[]): boolean {
1426
if (!publicKey) {
1527
throw new Error("You must provide a public key for verify with ED25519");
@@ -23,6 +35,12 @@ export class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
2335
return sha512Ed25519DigitalSignatureVerify(publicKey, dataToVerify, signature);
2436
}
2537

38+
/**
39+
* Generates and RSA digital signature with SHA3-512
40+
* @param rsa_key_size
41+
* @param data_to_sign
42+
* @returns RsaDigitalSignatureResult
43+
*/
2644
createRsa(rsa_key_size: number, data_to_sign: number[]): RsaDigitalSignatureResult {
2745
if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) {
2846
throw new Error("You need to provide an appropriate RSA key size.");
@@ -33,6 +51,13 @@ export class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
3351
return sha512RsaDigitalSignature(rsa_key_size, data_to_sign);
3452
}
3553

54+
/**
55+
* Verifies a digital signature created with the RSA public key.
56+
* @param public_key
57+
* @param data_to_verify
58+
* @param signature
59+
* @returns boolean
60+
*/
3661
verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean {
3762
if (!public_key) {
3863
throw new Error("Must provide a public key");

src-ts/digital-signature/digital-signature-factory.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export enum DigitalSignatureType {
77
}
88

99
export class DigitalSignatureFactory {
10+
11+
/**
12+
* Get the appropriate digital signature wrapper based upon the type passed in.
13+
* @param type
14+
* @returns
15+
*/
1016
public static get(type: DigitalSignatureType) {
1117
let ds = new DigitalSignatureSHA512Wrapper();
1218
switch (type) {

src-ts/digital-signature/digital-signaturte-sha-256.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ import { IDigitalSignature } from "./digital-signature-base";
33

44
export class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
55

6+
/**
7+
* Creates an ED25519 siganture from an array of bytes with SHA3-512.
8+
* @param dataToSign
9+
* @returns SHAED25519DalekDigitalSignatureResult
10+
*/
611
createED25519(dataToSign: number[]): Shaed25519DalekDigitalSignatureResult {
712
if (dataToSign?.length === 0) {
813
throw new Error("Must provide allocated data to sign");
914
}
1015
return sha256Ed25519DigitalSignature(dataToSign);
1116
}
1217

18+
/**
19+
* Verifies an ED25519 signature with the public key generated from running createED25519() with SHA3-512
20+
* @param publicKey
21+
* @param dataToVerify
22+
* @param signature
23+
* @returns boolean
24+
*/
1325
verifyED25519(publicKey: number[], dataToVerify: number[], signature: number[]): boolean {
1426
if (!publicKey) {
1527
throw new Error("You must provide a public key for verify with ED25519");
@@ -23,6 +35,12 @@ export class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
2335
return sha256Ed25519DigitalSignatureVerify(publicKey, dataToVerify, signature);
2436
}
2537

38+
/**
39+
* Generates and RSA digital signature with SHA3-512
40+
* @param rsa_key_size
41+
* @param data_to_sign
42+
* @returns RsaDigitalSignatureResult
43+
*/
2644
createRsa(rsa_key_size: number, data_to_sign: number[]): RsaDigitalSignatureResult {
2745
if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) {
2846
throw new Error("You need to provide an appropriate RSA key size.");
@@ -33,6 +51,13 @@ export class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
3351
return sha256RsaDigitalSignature(rsa_key_size, data_to_sign);
3452
}
3553

54+
/**
55+
* Verifies a digital signature created with the RSA public key.
56+
* @param public_key
57+
* @param data_to_verify
58+
* @param signature
59+
* @returns boolean
60+
*/
3661
verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean {
3762
if (!public_key) {
3863
throw new Error("Must provide a public key");

src-ts/hashers/hasher-factory.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { HasherType } from "./hasher-type";
22
import { SHAWrapper } from "./sha-wrapper";
33

44
export class HasherFactory {
5+
/**
6+
* Get the appropriate hasher wrapper based upon the type based in.
7+
* @param type
8+
* @returns
9+
*/
510
getHasher(type: HasherType): any {
611
let result: SHAWrapper = new SHAWrapper();
712
switch(type) {

src-ts/hashers/sha-wrapper.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ import { sha256, sha256Verify, sha512, sha512Verify } from "../../index";
22
import { IHasherBase } from "./hasher-base";
33

44
export class SHAWrapper implements IHasherBase {
5+
/**
6+
* Hashes a byte array with SHA3-512.
7+
* @param dataToHash
8+
* @returns number[]
9+
*/
510
hash512(dataToHash: number[]): number[] {
611
if (!dataToHash || dataToHash.length === 0) {
712
throw new Error("You must provide an allocated array of data");
813
}
914
return sha512(dataToHash);
1015
}
1116

17+
/**
18+
* Verifies unsigned data against an SHA3-512 hash.
19+
* @param dataToHash
20+
* @param dataToVerify
21+
* @returns boolean
22+
*/
1223
verify512(dataToHash: number[], dataToVerify: number[]): boolean {
1324
if (!dataToHash || dataToHash.length === 0) {
1425
throw new Error("You must provide an allocated array of data");
@@ -19,13 +30,24 @@ export class SHAWrapper implements IHasherBase {
1930
return sha512Verify(dataToHash, dataToVerify);
2031
}
2132

33+
/**
34+
* Hashes a byte array with SHA3-256.
35+
* @param dataToHash
36+
* @returns number[]
37+
*/
2238
hash256(dataToHash: number[]): number[] {
2339
if (!dataToHash || dataToHash.length === 0) {
2440
throw new Error("You must provide an allocated array of data");
2541
}
2642
return sha256(dataToHash);
2743
}
2844

45+
/**
46+
* Verifies unsigned data against an SHA3-256 hash.
47+
* @param dataToHash
48+
* @param dataToVerify
49+
* @returns boolean
50+
*/
2951
verify256(dataToHash: number[], dataToVerify: number[]): boolean {
3052
if (!dataToHash || dataToHash.length === 0) {
3153
throw new Error("You must provide an allocated array of data");

src-ts/helpers/nonce-generator.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src-ts/hybrid/hybrid-encryption-wrapper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export class HybridEncryptionWrapper {
1212
this.rsaWrapper = new RSAWrapper();
1313
}
1414

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+
*/
1521
public encrypt(
1622
dataToEncrypt: Array<number>,
1723
initalizer: AESRSAHybridInitializer,
@@ -40,6 +46,12 @@ export class HybridEncryptionWrapper {
4046
return result;
4147
}
4248

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+
*/
4355
public decrypt(
4456
privateKey: string,
4557
encryptResult: AesRsaHybridEncryptResult,

0 commit comments

Comments
 (0)