Skip to content

Commit a45b4e3

Browse files
authored
Merge pull request #23 from Cryptographic-API-Services/#22-password-hashing-threading
#22 password hashing threading
2 parents 9d1eb3d + 90159d1 commit a45b4e3

File tree

14 files changed

+375
-25
lines changed

14 files changed

+375
-25
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ sha3 = "0.10.8"
2222
x25519-dalek = {version = "2.0.0", features = ["static_secrets"]}
2323
rand_07 = { package = "rand", version = "0.7.0" }
2424
ascon-aead = "0.4.2"
25+
rayon = "1.10.0"
2526

2627
[profile.dev.package.num-bigint-dig]
2728
opt-level = 3

docs/EXAMPLES.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,105 @@ const ciphertext = aesWrapper.aes128Encrypt(aesKey, aesNonce, tohashBytes);
1111
const plaintxt = aesWrapper.aes128Decrypt(aesKey, aesNonce, ciphertext);
1212
```
1313

14+
### Asymmetric
15+
-RSA
16+
```typescript
17+
const rsaWrapper: RSAWrapper = new RSAWrapper();
18+
const keys: RsaKeyPairResult = rsaWrapper.generateKeys(4096);
19+
const tohashed: string = "This is my array to encrypt";
20+
const encoder = new TextEncoder();
21+
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
22+
const ciphertext = rsaWrapper.encrypt(keys.publicKey, tohashBytes);
23+
const plaintext = rsaWrapper.decrypt(keys.privateKey, ciphertext);
24+
```
25+
26+
27+
### Digital Signature
28+
-ED25519 SHA
29+
```typescript
30+
const shaDsWrapper = DigitalSignatureFactory.get(DigitalSignatureType.SHA256)
31+
const toHash: string = "This is my array to encrypt";
32+
const encoder = new TextEncoder();
33+
const toHashBytes: Array<number> = Array.from(encoder.encode(toHash));
34+
const dsResult = shaDsWrapper.createED25519(toHashBytes);
35+
const verify = shaDsWrapper.verifyED25519(dsResult.publicKey, toHashBytes, dsResult.signature);
36+
```
37+
38+
-RSA SHA
39+
```typescript
40+
const shaDsWrapper = DigitalSignatureFactory.get(DigitalSignatureType.SHA512)
41+
const tohashed: string = "This is my array to encrypt";
42+
const notOriginal: string = "This is not a fun time";
43+
const encoder = new TextEncoder();
44+
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
45+
const badBytes: Array<number> = Array.from(encoder.encode(notOriginal));
46+
const dsResult: RSADigitalSignatureResult = shaDsWrapper.createRsa(4096, tohashBytes);
47+
const verify = shaDsWrapper.verifyRSa(dsResult.publicKey, badBytes, dsResult.signature);
48+
```
49+
50+
51+
### Hashers
52+
-SHA3 512
53+
```typescript
54+
const wrapper = new SHAWrapper();
55+
const tohashed: string = "This is my array to hash";
56+
const encoder = new TextEncoder();
57+
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
58+
const hashed = wrapper.hash512(tohashBytes);
59+
```
60+
61+
-SHA3 256
62+
```typescript
63+
const wrapper = new SHAWrapper();
64+
const tohashed: string = "This is my array to hash";
65+
const encoder = new TextEncoder();
66+
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
67+
const hashed = wrapper.hash256(tohashBytes);
68+
```
69+
70+
### Hybrid Encryption
71+
-AES/RSA Encryption
72+
```typescript
73+
const hybridWrapper = new HybridEncryptionWrapper();
74+
let initalizer = new AESRSAHybridInitializer(128, 4096);
75+
const tohashed: string = "This is my encrypt text for rsa hybrid";
76+
const encoder = new TextEncoder();
77+
const toEncrypt: Array<number> = Array.from(encoder.encode(tohashed));
78+
let result: AesRsaHybridEncryptResult = hybridWrapper.encrypt(toEncrypt, initalizer);
79+
let plaintext: Array<number> = hybridWrapper.decrypt(initalizer.rsaKeyPair.privateKey, result);
80+
```
81+
82+
### Key Exchange
83+
-X25519
84+
```typescript
85+
const wrapper = new X25519Wrapper();
86+
const alice = wrapper.generateSecretAndPublicKey();
87+
const bob = wrapper.generateSecretAndPublicKey();
88+
89+
const alice_shared_secret = wrapper.generateSharedSecret(
90+
alice.secretKey,
91+
bob.publicKey,
92+
);
93+
const bob_shared_secret = wrapper.generateSharedSecret(
94+
bob.secretKey,
95+
alice.publicKey,
96+
);
97+
98+
var result = areEqual(alice_shared_secret, bob_shared_secret);
99+
```
100+
101+
### Sponges
102+
-Ascon 128
103+
```typescript
104+
const wrapper: AsconWrapper = new AsconWrapper();
105+
const key: Array<number> = wrapper.ascon128Key();
106+
const nonce: Array<number> = wrapper.ascon128Nonce();
107+
const tohashed: string = "This is my array to encrypt";
108+
const encoder = new TextEncoder();
109+
const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
110+
const ciphertext = wrapper.ascon128Encrypt(key, nonce, tohashBytes);
111+
const plaintext = wrapper.ascon128Decrypt(key, nonce, ciphertext);
112+
```
14113

15114
### Passwords
16115
- BCrypt

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
/* auto-generated by NAPI-RS */
55

66
export function argon2Hash(password: string): string
7+
export function argon2HashThreadPool(password: string): string
78
export function argon2Verify(hashedPassword: string, passwordToVerify: string): boolean
9+
export function argon2VerifyThreadpool(hashedPassword: string, passwordToVerify: string): boolean
810
export function bcryptHash(passwordToHash: string): string
11+
export function bcryptHashThreadpool(passwordToHash: string): string
912
export function bcryptVerify(hashedPassword: string, passwordToVerify: string): boolean
13+
export function bcryptVerifyThreadpool(passwordToHash: string, passwordToVerify: string): boolean
1014
export function scryptHash(passwordToHash: string): string
1115
export function scryptVerify(hashedPassword: string, passwordToVerify: string): boolean
16+
export function scryptHashThreadpool(passwordToHash: string): string
17+
export function scryptVerifyThreadpool(hashedPassword: string, passwordToVerify: string): boolean
1218
export function sha512(dataToHash: Array<number>): Array<number>
1319
export function sha512Verify(dataToHash: Array<number>, dataToVerify: Array<number>): boolean
1420
export function sha256(dataToHash: Array<number>): Array<number>

index.node

158 KB
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.19",
4+
"version": "1.0.22",
55
"description": "",
66
"main": "lib/index.js",
77
"types": "lib/index.d.ts",

src-ts/password-hashers/argon2-wrapper.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1-
import {argon2Hash, argon2Verify} from "./../../index";
2-
import { IPasswordHasherBase} from "./password-hasher-base";
1+
import { argon2Hash, argon2HashThreadPool, argon2Verify, argon2VerifyThreadpool } from "./../../index";
2+
import { IPasswordHasherBase } from "./password-hasher-base";
33

44
export class Argon2Wrapper implements IPasswordHasherBase {
5+
6+
verifyThreadPool(hashedPassword: string, passwordToCheck: string): boolean {
7+
if (!hashedPassword) {
8+
throw new Error("You must provide a password to verify with Argon2");
9+
}
10+
if (!passwordToCheck) {
11+
throw new Error("You must provide a password to check to verify with Argon2");
12+
}
13+
return argon2VerifyThreadpool(hashedPassword, passwordToCheck);
14+
}
15+
16+
public hashPasswordThreadPool(password: string): string {
17+
if (!password) {
18+
throw new Error("You must provide a password to hash with Argon2");
19+
}
20+
return argon2HashThreadPool(password);
21+
}
22+
523
public hashPassword(password: string): string {
6-
if (!password){
24+
if (!password) {
725
throw new Error("You must provide a password to hash with Argon2");
826
}
927
return argon2Hash(password);
1028
}
1129

1230
public verify(hashedPassword: string, passwordToVerify: string): boolean {
1331
if (!hashedPassword || !passwordToVerify) {
14-
throw new Error("You must provide a hashed password and a plaintext password to verify with Argon2");
32+
throw new Error(
33+
"You must provide a hashed password and a plaintext password to verify with Argon2",
34+
);
1535
}
1636
return argon2Verify(hashedPassword, passwordToVerify);
1737
}

src-ts/password-hashers/bcrypt-wrapper.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
import { IPasswordHasherBase } from "./password-hasher-base";
2-
import { bcryptHash, bcryptVerify } from "./../../index";
2+
import { bcryptHash, bcryptHashThreadpool, bcryptVerify, bcryptVerifyThreadpool } from "./../../index";
33

44
export class BCryptWrapper implements IPasswordHasherBase {
5+
6+
verifyThreadPool(hashedPassword: string, passwordToCheck: string): boolean {
7+
if (!hashedPassword || !passwordToCheck) {
8+
throw new Error(
9+
"You must provide a hashed password and a plaintext password to verify with Argon2",
10+
);
11+
}
12+
return bcryptVerifyThreadpool(hashedPassword, passwordToCheck);
13+
}
14+
15+
public hashPasswordThreadPool(password: string): string {
16+
if (!password) {
17+
throw new Error("You must provide a password to hash with Argon2");
18+
}
19+
return bcryptHashThreadpool(password);
20+
}
21+
522
public hashPassword(password: string): string {
623
if (!password) {
724
throw new Error("You must provide a password to hash with Argon2");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export interface IPasswordHasherBase {
22
hashPassword(password: string): string;
33
verify(hashedPassword: string, passwordToVerify: string): boolean;
4+
hashPasswordThreadPool(password: string): string;
5+
verifyThreadPool(hashedPassword: string, passwordToCheck: string): boolean;
46
}
Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
import { scryptHash, scryptVerify } from "../../index";
1+
import { scryptHash, scryptHashThreadpool, scryptVerify, scryptVerifyThreadpool } from "../../index";
22
import { IPasswordHasherBase } from "./password-hasher-base";
33

44
export class ScryptWrapper implements IPasswordHasherBase {
55

6-
public hashPassword(password: string): string {
7-
if (!password){
8-
throw new Error("You must provide a password to hash with Scrypt");
9-
}
10-
return scryptHash(password);
11-
}
12-
13-
public verify(hashedPassword: string, passwordToVerify: string): boolean {
14-
if (!hashedPassword || !passwordToVerify) {
15-
throw new Error("You must provide a hashed password and a plaintext password to verify with Scrypt");
16-
}
17-
return scryptVerify(hashedPassword, passwordToVerify);
18-
}
6+
verifyThreadPool(hashedPassword: string, passwordToCheck: string): boolean {
7+
if (!hashedPassword || !passwordToCheck) {
8+
throw new Error(
9+
"You must provide a hashed password and a plaintext password to verify with Scrypt",
10+
);
11+
}
12+
return scryptVerifyThreadpool(hashedPassword, passwordToCheck);
13+
}
1914

20-
}
15+
hashPasswordThreadPool(password: string): string {
16+
if (!password) {
17+
throw new Error("You must provide a password to hash with Scrypt");
18+
}
19+
return scryptHashThreadpool(password);
20+
}
21+
22+
public hashPassword(password: string): string {
23+
if (!password) {
24+
throw new Error("You must provide a password to hash with Scrypt");
25+
}
26+
return scryptHash(password);
27+
}
28+
29+
public verify(hashedPassword: string, passwordToVerify: string): boolean {
30+
if (!hashedPassword || !passwordToVerify) {
31+
throw new Error(
32+
"You must provide a hashed password and a plaintext password to verify with Scrypt",
33+
);
34+
}
35+
return scryptVerify(hashedPassword, passwordToVerify);
36+
}
37+
}

src-ts/symmetric/aes-wrapper.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,32 @@ import {
1111
aesNonce,
1212
} from "../../index";
1313

14+
/**
15+
* @description A wrapper class that contains methods to construct keys, nonces, and methods to encrypt and decrypt with AES-128-GCM and AES-256-GCM
16+
*
17+
* @example
18+
* ```ts
19+
* const nonce = aesWrapper.generateAESNonce();
20+
const key = aesWrapper.aes128Key();
21+
const textEncoder = new TextEncoder();
22+
const array = Array.from(textEncoder.encode("Hello World"));
23+
const encrypted = aesWrapper.aes128Encrypt(key, nonce, array);
24+
* ```
25+
*/
1426
export class AESWrapper {
27+
28+
/**
29+
* @description Generates a 128 bit AES key
30+
* @returns returns a 128 bit AES key
31+
*/
1532
public aes128Key(): Array<number> {
1633
return aes128Key();
1734
}
1835

36+
/**
37+
* @description Generates a 256 bit AES key
38+
* @returns returns a 256 bit AES key
39+
*/
1940
public aes256Key(): Array<number> {
2041
return aes256Key();
2142
}

0 commit comments

Comments
 (0)