Skip to content

Commit 90159d1

Browse files
committed
thread pool for scrypt
1 parent a922a56 commit 90159d1

File tree

6 files changed

+73
-5
lines changed

6 files changed

+73
-5
lines changed

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export function bcryptVerify(hashedPassword: string, passwordToVerify: string):
1313
export function bcryptVerifyThreadpool(passwordToHash: string, passwordToVerify: string): boolean
1414
export function scryptHash(passwordToHash: string): string
1515
export function scryptVerify(hashedPassword: string, passwordToVerify: string): boolean
16+
export function scryptHashThreadpool(passwordToHash: string): string
17+
export function scryptVerifyThreadpool(hashedPassword: string, passwordToVerify: string): boolean
1618
export function sha512(dataToHash: Array<number>): Array<number>
1719
export function sha512Verify(dataToHash: Array<number>, dataToVerify: Array<number>): boolean
1820
export function sha256(dataToHash: Array<number>): Array<number>

index.node

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

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
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

66
verifyThreadPool(hashedPassword: string, passwordToCheck: string): boolean {
7-
throw new Error("Method not implemented.");
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);
813
}
9-
14+
1015
hashPasswordThreadPool(password: string): string {
11-
throw new Error("Method not implemented.");
16+
if (!password) {
17+
throw new Error("You must provide a password to hash with Scrypt");
18+
}
19+
return scryptHashThreadpool(password);
1220
}
1321

1422
public hashPassword(password: string): string {

src/password_hashers/scrypt.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::mpsc;
2+
13
use napi_derive::napi;
24

35
use scrypt::{
@@ -36,6 +38,43 @@ pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> boo
3638
return <CASScrypt as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
3739
}
3840

41+
#[napi]
42+
pub fn scrypt_hash_threadpool(password_to_hash: String) -> String {
43+
let (sender, receiver) = mpsc::channel();
44+
rayon::spawn(move || {
45+
let thread_result = <CASScrypt as CASPasswordHasher>::hash_password(password_to_hash);
46+
sender.send(thread_result);
47+
});
48+
let result = receiver.recv().unwrap();
49+
result
50+
}
51+
52+
#[napi]
53+
pub fn scrypt_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool {
54+
let (sender, receiver) = mpsc::channel();
55+
rayon::spawn(move || {
56+
let thread_result = <CASScrypt as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
57+
sender.send(thread_result);
58+
});
59+
let result = receiver.recv().unwrap();
60+
result
61+
}
62+
63+
#[test]
64+
pub fn scrypt_hash_threadpool_test() {
65+
let password = "BadPassword".to_string();
66+
let hashed_password = scrypt_hash_threadpool(password.clone());
67+
assert_ne!(password, hashed_password);
68+
}
69+
70+
#[test]
71+
pub fn scrypt_verify_threadpool_test() {
72+
let password = "BadPassword".to_string();
73+
let hashed_password = scrypt_hash_threadpool(password.clone());
74+
let verified = scrypt_verify_threadpool(hashed_password, password);
75+
assert_eq!(true, verified);
76+
}
77+
3978
#[test]
4079
pub fn scrypt_hash_test() {
4180
let password = "BadPassword".to_string();

test-ts/password-hasher.test.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ describe("Bcrypt Tests", () => {
5151
});
5252

5353
describe("Scrypt Tests", () => {
54+
it("hash with threadpool", () => {
55+
const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
56+
PasswordHasherType.Scrypt,
57+
);
58+
const password: string = "ScryptRocks";
59+
const hashed: string = hasher.hashPasswordThreadPool(password);
60+
assert.notEqual(password, hashed);
61+
});
62+
63+
it("verify pass with threadpool", () => {
64+
const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
65+
PasswordHasherType.Scrypt,
66+
);
67+
const password: string = "ScryptRocks1231231";
68+
const hashed: string = hasher.hashPasswordThreadPool(password);
69+
const verified: boolean = hasher.verifyThreadPool(hashed, password);
70+
assert.isTrue(verified);
71+
});
72+
5473
it("hash with factory", () => {
5574
const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
5675
PasswordHasherType.Scrypt,

0 commit comments

Comments
 (0)