cas-typescript-sdk 1.0.8 → 1.0.9

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/Cargo.toml CHANGED
@@ -14,6 +14,7 @@ napi = "2"
14
14
  napi-derive = "2"
15
15
  rand = "0.8.5"
16
16
  scrypt = "0.11.0"
17
+ sha3 = "0.10.8"
17
18
 
18
19
  [build-dependencies]
19
20
  napi-build = "1"
package/README.md CHANGED
@@ -7,5 +7,12 @@ The official NPM page can be found [here](https://www.npmjs.com/package/cas-type
7
7
 
8
8
  This Node.js NPM module is dependent on our Rust layer [here](./src) that contains methods to run industry standard cryptographic operations sequentially, on threads, and the thread pool.
9
9
 
10
+ ## Consuming Library Documentation
11
+ We utilize some smart people's existing work and we believe their documentation should be reviewed when possible.
12
+ - [Spin Research](https://github.com/SpinResearch)
13
+ - [Dalek-Cryptography](https://github.com/dalek-cryptography)
14
+ - [Rust Crypto](https://github.com/RustCrypto)
15
+ - [Rayon](https://github.com/rayon-rs/rayon)
16
+
10
17
  ## Disclaimer
11
- Many of the cryptographic crates that are utilized in our core FFI [layer](https://github.com/Crytographic-API-Services/cas-core-lib) have never had a security audit performed. Utilize this SDK at your own risk.
18
+ Many of the cryptographic crates that are utilized in our core FFI [layer](./src) have never had a security audit performed. Utilize this SDK at your own risk.
package/index.d.ts CHANGED
@@ -9,3 +9,7 @@ export function bcryptHash(passwordToHash: string): string
9
9
  export function bcryptVerify(hashedPassword: string, passwordToVerify: string): boolean
10
10
  export function scryptHash(passwordToHash: string): string
11
11
  export function scryptVerify(hashedPassword: string, passwordToVerify: string): boolean
12
+ export function sha512(dataToHash: Array<number>): Array<number>
13
+ export function sha512Verify(dataToHash: Array<number>, dataToVerify: Array<number>): boolean
14
+ export function sha256(dataToHash: Array<number>): Array<number>
15
+ export function sha256Verify(dataToHash: Array<number>, dataToVerify: Array<number>): boolean
package/index.node CHANGED
Binary file
@@ -0,0 +1,6 @@
1
+ export interface IHasherBase {
2
+ hash_512(dataToHash: number[]): number[];
3
+ verify_512(dataToHash: number[], dataToVerify: number[]): boolean;
4
+ hash_256(dataToHash: number[]): number[];
5
+ verify_256(dataToHash: number[], dataToVerify: number[]): boolean;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { IHasherBase } from "./IHasherBase";
2
+ export declare class SHAWrapper implements IHasherBase {
3
+ hash_512(dataToHash: number[]): number[];
4
+ verify_512(dataToHash: number[], dataToVerify: number[]): boolean;
5
+ hash_256(dataToHash: number[]): number[];
6
+ verify_256(dataToHash: number[], dataToVerify: number[]): boolean;
7
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SHAWrapper = void 0;
4
+ const index_1 = require("../../index");
5
+ class SHAWrapper {
6
+ hash_512(dataToHash) {
7
+ if (!dataToHash || dataToHash.length === 0) {
8
+ throw new Error("You must provide an allocated array of data");
9
+ }
10
+ return (0, index_1.sha512)(dataToHash);
11
+ }
12
+ verify_512(dataToHash, dataToVerify) {
13
+ if (!dataToHash || dataToHash.length === 0) {
14
+ throw new Error("You must provide an allocated array of data");
15
+ }
16
+ if (!dataToVerify || dataToVerify.length === 0) {
17
+ throw new Error("You must provide an allocated array of data to verify");
18
+ }
19
+ return (0, index_1.sha512Verify)(dataToHash, dataToVerify);
20
+ }
21
+ hash_256(dataToHash) {
22
+ if (!dataToHash || dataToHash.length === 0) {
23
+ throw new Error("You must provide an allocated array of data");
24
+ }
25
+ return (0, index_1.sha256)(dataToHash);
26
+ }
27
+ verify_256(dataToHash, dataToVerify) {
28
+ if (!dataToHash || dataToHash.length === 0) {
29
+ throw new Error("You must provide an allocated array of data");
30
+ }
31
+ if (!dataToVerify || dataToVerify.length === 0) {
32
+ throw new Error("You must provide an allocated array of data to verify");
33
+ }
34
+ return (0, index_1.sha256Verify)(dataToHash, dataToVerify);
35
+ }
36
+ }
37
+ exports.SHAWrapper = SHAWrapper;
@@ -0,0 +1,6 @@
1
+ export interface IHasherBase {
2
+ hash_512(dataToHash: number[]): number[];
3
+ verify_512(dataToHash: number[], dataToVerify: number[]): boolean;
4
+ hash_256(dataToHash: number[]): number[];
5
+ verify_256(dataToHash: number[], dataToVerify: number[]): boolean;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import { HasherType } from "./hasher-type";
2
+ export declare class HasherFactory {
3
+ getHasher(type: HasherType): any;
4
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HasherFactory = void 0;
4
+ const sha_wrapper_1 = require("./sha-wrapper");
5
+ class HasherFactory {
6
+ getHasher(type) {
7
+ let result = new sha_wrapper_1.SHAWrapper();
8
+ switch (type) {
9
+ }
10
+ return result;
11
+ }
12
+ }
13
+ exports.HasherFactory = HasherFactory;
@@ -0,0 +1,3 @@
1
+ export declare enum HasherType {
2
+ SHA = 1
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HasherType = void 0;
4
+ var HasherType;
5
+ (function (HasherType) {
6
+ HasherType[HasherType["SHA"] = 1] = "SHA";
7
+ })(HasherType || (exports.HasherType = HasherType = {}));
@@ -0,0 +1,4 @@
1
+ import { HasherFactory } from "./hasher-factory";
2
+ import { HasherType } from "./hasher-type";
3
+ import { SHAWrapper } from "./sha-wrapper";
4
+ export { SHAWrapper, HasherFactory, HasherType };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HasherType = exports.HasherFactory = exports.SHAWrapper = void 0;
4
+ const hasher_factory_1 = require("./hasher-factory");
5
+ Object.defineProperty(exports, "HasherFactory", { enumerable: true, get: function () { return hasher_factory_1.HasherFactory; } });
6
+ const hasher_type_1 = require("./hasher-type");
7
+ Object.defineProperty(exports, "HasherType", { enumerable: true, get: function () { return hasher_type_1.HasherType; } });
8
+ const sha_wrapper_1 = require("./sha-wrapper");
9
+ Object.defineProperty(exports, "SHAWrapper", { enumerable: true, get: function () { return sha_wrapper_1.SHAWrapper; } });
@@ -0,0 +1,7 @@
1
+ import { IHasherBase } from "./hasher-base";
2
+ export declare class SHAWrapper implements IHasherBase {
3
+ hash_512(dataToHash: number[]): number[];
4
+ verify_512(dataToHash: number[], dataToVerify: number[]): boolean;
5
+ hash_256(dataToHash: number[]): number[];
6
+ verify_256(dataToHash: number[], dataToVerify: number[]): boolean;
7
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SHAWrapper = void 0;
4
+ const index_1 = require("../../index");
5
+ class SHAWrapper {
6
+ hash_512(dataToHash) {
7
+ if (!dataToHash || dataToHash.length === 0) {
8
+ throw new Error("You must provide an allocated array of data");
9
+ }
10
+ return (0, index_1.sha512)(dataToHash);
11
+ }
12
+ verify_512(dataToHash, dataToVerify) {
13
+ if (!dataToHash || dataToHash.length === 0) {
14
+ throw new Error("You must provide an allocated array of data");
15
+ }
16
+ if (!dataToVerify || dataToVerify.length === 0) {
17
+ throw new Error("You must provide an allocated array of data to verify");
18
+ }
19
+ return (0, index_1.sha512Verify)(dataToHash, dataToVerify);
20
+ }
21
+ hash_256(dataToHash) {
22
+ if (!dataToHash || dataToHash.length === 0) {
23
+ throw new Error("You must provide an allocated array of data");
24
+ }
25
+ return (0, index_1.sha256)(dataToHash);
26
+ }
27
+ verify_256(dataToHash, dataToVerify) {
28
+ if (!dataToHash || dataToHash.length === 0) {
29
+ throw new Error("You must provide an allocated array of data");
30
+ }
31
+ if (!dataToVerify || dataToVerify.length === 0) {
32
+ throw new Error("You must provide an allocated array of data to verify");
33
+ }
34
+ return (0, index_1.sha256Verify)(dataToHash, dataToVerify);
35
+ }
36
+ }
37
+ exports.SHAWrapper = SHAWrapper;
package/lib/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- import * as PasswordHashers from "./password-hashers/index";
2
- export { PasswordHashers };
1
+ import { Argon2Wrapper, BCryptWrapper, PasswordHasherFactory, PasswordHasherType, ScryptWrapper } from "./password-hashers/index";
2
+ import { HasherFactory, HasherType, SHAWrapper } from "./hashers/index";
3
+ export { Argon2Wrapper, BCryptWrapper, ScryptWrapper, PasswordHasherFactory, PasswordHasherType, HasherFactory, HasherType, SHAWrapper, };
package/lib/index.js CHANGED
@@ -1,28 +1,13 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.PasswordHashers = void 0;
27
- const PasswordHashers = __importStar(require("./password-hashers/index"));
28
- exports.PasswordHashers = PasswordHashers;
3
+ exports.SHAWrapper = exports.HasherType = exports.HasherFactory = exports.PasswordHasherType = exports.PasswordHasherFactory = exports.ScryptWrapper = exports.BCryptWrapper = exports.Argon2Wrapper = void 0;
4
+ const index_1 = require("./password-hashers/index");
5
+ Object.defineProperty(exports, "Argon2Wrapper", { enumerable: true, get: function () { return index_1.Argon2Wrapper; } });
6
+ Object.defineProperty(exports, "BCryptWrapper", { enumerable: true, get: function () { return index_1.BCryptWrapper; } });
7
+ Object.defineProperty(exports, "PasswordHasherFactory", { enumerable: true, get: function () { return index_1.PasswordHasherFactory; } });
8
+ Object.defineProperty(exports, "PasswordHasherType", { enumerable: true, get: function () { return index_1.PasswordHasherType; } });
9
+ Object.defineProperty(exports, "ScryptWrapper", { enumerable: true, get: function () { return index_1.ScryptWrapper; } });
10
+ const index_2 = require("./hashers/index");
11
+ Object.defineProperty(exports, "HasherFactory", { enumerable: true, get: function () { return index_2.HasherFactory; } });
12
+ Object.defineProperty(exports, "HasherType", { enumerable: true, get: function () { return index_2.HasherType; } });
13
+ Object.defineProperty(exports, "SHAWrapper", { enumerable: true, get: function () { return index_2.SHAWrapper; } });
@@ -3,4 +3,4 @@ import { BCryptWrapper } from "./bcrypt-wrapper";
3
3
  import { ScryptWrapper } from "./scrypt-wrapper";
4
4
  import { PasswordHasherType } from "./password-hasher-type";
5
5
  import { PasswordHasherFactory } from "./password-hasher-factory";
6
- export { PasswordHasherFactory, PasswordHasherType, BCryptWrapper, Argon2Wrapper, ScryptWrapper };
6
+ export { Argon2Wrapper, BCryptWrapper, PasswordHasherFactory, PasswordHasherType, ScryptWrapper, };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ScryptWrapper = exports.Argon2Wrapper = exports.BCryptWrapper = exports.PasswordHasherType = exports.PasswordHasherFactory = void 0;
3
+ exports.ScryptWrapper = exports.PasswordHasherType = exports.PasswordHasherFactory = exports.BCryptWrapper = exports.Argon2Wrapper = void 0;
4
4
  const argon2_wrapper_1 = require("./argon2-wrapper");
5
5
  Object.defineProperty(exports, "Argon2Wrapper", { enumerable: true, get: function () { return argon2_wrapper_1.Argon2Wrapper; } });
6
6
  const bcrypt_wrapper_1 = require("./bcrypt-wrapper");
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "cas-typescript-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "scripts": {
8
- "test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
8
+ "test": "cargo test && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
9
9
  "build": "napi build --release && tsc",
10
10
  "prepare": "npm run build"
11
11
  },
@@ -0,0 +1,8 @@
1
+ use napi::bindgen_prelude::Array;
2
+
3
+ pub trait CASHasher {
4
+ fn hash_512(data_to_hash: Vec<u8>) -> Vec<u8>;
5
+ fn verify_512(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool;
6
+ fn hash_256(data_to_hash: Vec<u8>) -> Vec<u8>;
7
+ fn verify_256(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool;
8
+ }
@@ -0,0 +1,103 @@
1
+ use napi::bindgen_prelude::Array;
2
+ use napi_derive::napi;
3
+
4
+ use super::cas_hasher::CASHasher;
5
+ use sha3::{Digest, Sha3_256, Sha3_512};
6
+ pub struct CASSHA;
7
+
8
+ impl CASHasher for CASSHA {
9
+ fn hash_512(data_to_hash: Vec<u8>) -> Vec<u8> {
10
+ let mut hasher = Sha3_512::new();
11
+ hasher.update(data_to_hash);
12
+ let result = hasher.finalize();
13
+ return result.to_vec();
14
+ }
15
+
16
+ fn verify_512(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
17
+ let mut hasher = Sha3_512::new();
18
+ hasher.update(data_to_verify);
19
+ let result = hasher.finalize();
20
+ return hash_to_verify.eq(&result.to_vec());
21
+ }
22
+
23
+ fn hash_256(data_to_hash: Vec<u8>) -> Vec<u8> {
24
+ let mut hasher = Sha3_256::new();
25
+ hasher.update(data_to_hash);
26
+ let result = hasher.finalize();
27
+ return result.to_vec();
28
+ }
29
+
30
+ fn verify_256(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
31
+ let mut hasher = Sha3_256::new();
32
+ hasher.update(data_to_verify);
33
+ let result = hasher.finalize();
34
+ return hash_to_verify.eq(&result.to_vec());
35
+ }
36
+ }
37
+
38
+ #[napi]
39
+ pub fn sha_512(data_to_hash: Vec<u8>) -> Vec<u8> {
40
+ return <CASSHA as CASHasher>::hash_512(data_to_hash);
41
+ }
42
+
43
+ #[napi]
44
+ pub fn sha_512_verify(data_to_hash: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
45
+ return <CASSHA as CASHasher>::verify_512(data_to_hash, data_to_verify);
46
+ }
47
+
48
+
49
+ #[napi]
50
+ pub fn sha_256(data_to_hash: Vec<u8>) -> Vec<u8> {
51
+ return <CASSHA as CASHasher>::hash_256(data_to_hash);
52
+ }
53
+
54
+ #[napi]
55
+ pub fn sha_256_verify(data_to_hash: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
56
+ return <CASSHA as CASHasher>::verify_256(data_to_hash, data_to_verify);
57
+ }
58
+
59
+ #[test]
60
+ pub fn sha_512_test() {
61
+ let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
62
+ let hashed_data = sha_512(data_to_hash.clone());
63
+ assert_ne!(true, hashed_data.eq(&data_to_hash));
64
+ }
65
+
66
+ #[test]
67
+ pub fn sha_512_verify_test() {
68
+ let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
69
+ let hashed_data = sha_512(data_to_hash.clone());
70
+ let data_to_verify = "NotMyDataToHash".as_bytes().to_vec();
71
+ assert_ne!(true, sha_512_verify(data_to_hash, data_to_verify));
72
+ }
73
+
74
+ #[test]
75
+ pub fn sha_512_verify_fail_test() {
76
+ let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
77
+ let hashed_data = sha_512(data_to_hash.clone());
78
+ let data_to_verify = "NotMyDataToHash2".as_bytes().to_vec();
79
+ assert_ne!(true, sha_512_verify(data_to_hash, data_to_verify));
80
+ }
81
+
82
+ #[test]
83
+ pub fn sha_256_test() {
84
+ let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
85
+ let hashed_data = sha_256(data_to_hash.clone());
86
+ assert_ne!(true, hashed_data.eq(&data_to_hash));
87
+ }
88
+
89
+ #[test]
90
+ pub fn sha_256_verify_test() {
91
+ let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
92
+ let hashed_data = sha_256(data_to_hash.clone());
93
+ let data_to_verify = "NotMyDataToHash".as_bytes().to_vec();
94
+ assert_ne!(true, sha_256_verify(data_to_hash, data_to_verify));
95
+ }
96
+
97
+ #[test]
98
+ pub fn sha_256_verify_fail_test() {
99
+ let data_to_hash = "NotMyDataToHash".as_bytes().to_vec();
100
+ let hashed_data = sha_256(data_to_hash.clone());
101
+ let data_to_verify = "NotMyDataToHash2".as_bytes().to_vec();
102
+ assert_ne!(true, sha_256_verify(data_to_hash, data_to_verify));
103
+ }
package/src/lib.rs CHANGED
@@ -3,4 +3,9 @@ mod password_hashers {
3
3
  pub mod bcrypt;
4
4
  pub mod scrypt;
5
5
  pub mod cas_password_hasher;
6
+ }
7
+
8
+ mod hashers {
9
+ pub mod sha;
10
+ pub mod cas_hasher;
6
11
  }
@@ -1,3 +1,5 @@
1
+ use std::net::ToSocketAddrs;
2
+
1
3
  use napi_derive::napi;
2
4
 
3
5
  use argon2::{
@@ -34,4 +36,29 @@ pub fn argon2_hash(password: String) -> String {
34
36
  #[napi]
35
37
  pub fn argon2_verify(hashed_password: String, password_to_verify: String) -> bool {
36
38
  return <CASArgon as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
39
+ }
40
+
41
+
42
+ #[test]
43
+ pub fn argon2_hash_test() {
44
+ let password = "ThisIsNotMyPasswolrd".to_string();
45
+ let hashed = argon2_hash(password.clone());
46
+ assert_ne!(password, hashed);
47
+ }
48
+
49
+ #[test]
50
+ pub fn argon2_verify_test() {
51
+ let password = "ThisIsNotMyPasswolrd".to_string();
52
+ let hashed = argon2_hash(password.clone());
53
+ let verified = argon2_verify(hashed, password);
54
+ assert_eq!(true, verified);
55
+ }
56
+
57
+ #[test]
58
+ pub fn argon2_verify_fail_test() {
59
+ let password = "ThisIsNotMyPasswolrd".to_string();
60
+ let hashed = argon2_hash(password.clone());
61
+ let verified = "Nope".to_string();
62
+ let verified = argon2_verify(hashed, verified);
63
+ assert_eq!(false, verified);
37
64
  }
@@ -24,4 +24,28 @@ pub fn bcrypt_hash(password_to_hash: String) -> String {
24
24
  #[napi]
25
25
  pub fn bcrypt_verify(hashed_password: String, password_to_verify: String) -> bool {
26
26
  return <CASBCrypt as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
27
+ }
28
+
29
+ #[test]
30
+ pub fn bcrypt_hash_test() {
31
+ let password = "ThisIsNotMyPasswolrd".to_string();
32
+ let hashed = bcrypt_hash(password.clone());
33
+ assert_ne!(password, hashed);
34
+ }
35
+
36
+ #[test]
37
+ pub fn bcrypt_verify_test() {
38
+ let password = "ThisIsNotMyPasswolrd".to_string();
39
+ let hashed = bcrypt_hash(password.clone());
40
+ let verified = bcrypt_verify(hashed, password);
41
+ assert_eq!(true, verified);
42
+ }
43
+
44
+ #[test]
45
+ pub fn bcrypt_verify_fail_test() {
46
+ let password = "ThisIsNotMyPasswolrd".to_string();
47
+ let hashed = bcrypt_hash(password.clone());
48
+ let verified = "nope".to_string();
49
+ let verified = bcrypt_verify(hashed, verified);
50
+ assert_eq!(false, verified);
27
51
  }
@@ -29,4 +29,28 @@ pub fn scrypt_hash(password_to_hash: String) -> String {
29
29
  #[napi]
30
30
  pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> bool {
31
31
  return <CASScrypt as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
32
+ }
33
+
34
+ #[test]
35
+ pub fn scrypt_hash_test() {
36
+ let password = "BadPassword".to_string();
37
+ let hashed_password = scrypt_hash(password.clone());
38
+ assert_ne!(password, hashed_password);
39
+ }
40
+
41
+ #[test]
42
+ pub fn scrypt_verify_test() {
43
+ let password = "BadPassword".to_string();
44
+ let hashed_password = scrypt_hash(password.clone());
45
+ let verified = scrypt_verify(hashed_password, password);
46
+ assert_eq!(true, verified);
47
+ }
48
+
49
+ #[test]
50
+ pub fn scrypt_verify_fail_test() {
51
+ let password = "BadPassword".to_string();
52
+ let hashed_password = scrypt_hash(password.clone());
53
+ let verified = "Nope".to_string();
54
+ let verified = scrypt_verify(hashed_password, verified);
55
+ assert_eq!(false, verified);
32
56
  }
@@ -0,0 +1,6 @@
1
+ export interface IHasherBase {
2
+ hash_512(dataToHash: number[]): number[];
3
+ verify_512(dataToHash: number[], dataToVerify: number[]): boolean;
4
+ hash_256(dataToHash: number[]): number[];
5
+ verify_256(dataToHash: number[], dataToVerify: number[]): boolean;
6
+ }
@@ -0,0 +1,12 @@
1
+ import { HasherType } from "./hasher-type";
2
+ import { SHAWrapper } from "./sha-wrapper";
3
+
4
+ export class HasherFactory {
5
+ getHasher(type: HasherType): any {
6
+ let result: SHAWrapper = new SHAWrapper();
7
+ switch(type) {
8
+
9
+ }
10
+ return result;
11
+ }
12
+ }
@@ -0,0 +1,3 @@
1
+ export enum HasherType {
2
+ SHA = 1
3
+ }
@@ -0,0 +1,5 @@
1
+ import { HasherFactory } from "./hasher-factory";
2
+ import { HasherType } from "./hasher-type";
3
+ import { SHAWrapper } from "./sha-wrapper";
4
+
5
+ export { SHAWrapper, HasherFactory, HasherType };
@@ -0,0 +1,38 @@
1
+ import { sha256, sha256Verify, sha512, sha512Verify } from "../../index";
2
+ import { IHasherBase } from "./hasher-base";
3
+
4
+ export class SHAWrapper implements IHasherBase {
5
+ hash_512(dataToHash: number[]): number[] {
6
+ if (!dataToHash || dataToHash.length === 0) {
7
+ throw new Error("You must provide an allocated array of data");
8
+ }
9
+ return sha512(dataToHash);
10
+ }
11
+
12
+ verify_512(dataToHash: number[], dataToVerify: number[]): boolean {
13
+ if (!dataToHash || dataToHash.length === 0) {
14
+ throw new Error("You must provide an allocated array of data");
15
+ }
16
+ if (!dataToVerify || dataToVerify.length === 0) {
17
+ throw new Error("You must provide an allocated array of data to verify");
18
+ }
19
+ return sha512Verify(dataToHash, dataToVerify);
20
+ }
21
+
22
+ hash_256(dataToHash: number[]): number[] {
23
+ if (!dataToHash || dataToHash.length === 0) {
24
+ throw new Error("You must provide an allocated array of data");
25
+ }
26
+ return sha256(dataToHash);
27
+ }
28
+
29
+ verify_256(dataToHash: number[], dataToVerify: number[]): boolean {
30
+ if (!dataToHash || dataToHash.length === 0) {
31
+ throw new Error("You must provide an allocated array of data");
32
+ }
33
+ if (!dataToVerify || dataToVerify.length === 0) {
34
+ throw new Error("You must provide an allocated array of data to verify");
35
+ }
36
+ return sha256Verify(dataToHash, dataToVerify);
37
+ }
38
+ }
package/src-ts/index.ts CHANGED
@@ -1,5 +1,19 @@
1
- import * as PasswordHashers from "./password-hashers/index";
1
+ import {
2
+ Argon2Wrapper,
3
+ BCryptWrapper,
4
+ PasswordHasherFactory,
5
+ PasswordHasherType,
6
+ ScryptWrapper,
7
+ } from "./password-hashers/index";
8
+ import { HasherFactory, HasherType, SHAWrapper } from "./hashers/index";
2
9
 
3
10
  export {
4
- PasswordHashers
5
- }
11
+ Argon2Wrapper,
12
+ BCryptWrapper,
13
+ ScryptWrapper,
14
+ PasswordHasherFactory,
15
+ PasswordHasherType,
16
+ HasherFactory,
17
+ HasherType,
18
+ SHAWrapper,
19
+ };
@@ -1,8 +1,14 @@
1
- import { Argon2Wrapper } from "./argon2-wrapper"
2
- import { BCryptWrapper } from "./bcrypt-wrapper"
3
- import { ScryptWrapper } from "./scrypt-wrapper"
1
+ import { Argon2Wrapper } from "./argon2-wrapper";
2
+ import { BCryptWrapper } from "./bcrypt-wrapper";
3
+ import { ScryptWrapper } from "./scrypt-wrapper";
4
4
 
5
- import { PasswordHasherType } from "./password-hasher-type"
6
- import { PasswordHasherFactory } from "./password-hasher-factory"
5
+ import { PasswordHasherType } from "./password-hasher-type";
6
+ import { PasswordHasherFactory } from "./password-hasher-factory";
7
7
 
8
- export {PasswordHasherFactory, PasswordHasherType, BCryptWrapper, Argon2Wrapper, ScryptWrapper}
8
+ export {
9
+ Argon2Wrapper,
10
+ BCryptWrapper,
11
+ PasswordHasherFactory,
12
+ PasswordHasherType,
13
+ ScryptWrapper,
14
+ };
@@ -0,0 +1,71 @@
1
+ import { assert } from "chai";
2
+ import { SHAWrapper } from "../src-ts/hashers/index";
3
+
4
+ describe("SHA512 Tests", () => {
5
+ it("hash", () => {
6
+ const wrapper = new SHAWrapper();
7
+ const tohashed: string = "This is my array to hash";
8
+ const encoder = new TextEncoder();
9
+ const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
10
+ const hashed = wrapper.hash_512(tohashBytes);
11
+ assert.notEqual(tohashBytes, hashed);
12
+ });
13
+
14
+ it("verify pass", () => {
15
+ const wrapper = new SHAWrapper();
16
+ const tohashed: string = "This is my array to hash";
17
+ const encoder = new TextEncoder();
18
+ const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
19
+ const hashed = wrapper.hash_512(tohashBytes);
20
+ const toVerifyBytes: Array<number> = Array.from(encoder.encode(tohashed));
21
+ const verified = wrapper.verify_512(hashed, toVerifyBytes);
22
+ assert.equal(true, verified);
23
+ });
24
+
25
+ it("verify fail", () => {
26
+ const wrapper = new SHAWrapper();
27
+ const tohashed: string = "This is my array to hash";
28
+ const encoder = new TextEncoder();
29
+ const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
30
+ const hashed = wrapper.hash_512(tohashBytes);
31
+ const toVerify = "This Is Not The Same";
32
+ const toVerifyBytes: Array<number> = Array.from(encoder.encode(toVerify));
33
+ const verified = wrapper.verify_512(hashed, toVerifyBytes);
34
+ assert.equal(false, verified);
35
+ });
36
+ });
37
+
38
+
39
+ describe("SHA256 Tests", () => {
40
+ it("hash", () => {
41
+ const wrapper = new SHAWrapper();
42
+ const tohashed: string = "This is my array to hash";
43
+ const encoder = new TextEncoder();
44
+ const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
45
+ const hashed = wrapper.hash_256(tohashBytes);
46
+ assert.notEqual(tohashBytes, hashed);
47
+ });
48
+
49
+ it("verify pass", () => {
50
+ const wrapper = new SHAWrapper();
51
+ const tohashed: string = "This is my array to hash";
52
+ const encoder = new TextEncoder();
53
+ const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
54
+ const hashed = wrapper.hash_256(tohashBytes);
55
+ const toVerifyBytes: Array<number> = Array.from(encoder.encode(tohashed));
56
+ const verified = wrapper.verify_256(hashed, toVerifyBytes);
57
+ assert.equal(true, verified);
58
+ });
59
+
60
+ it("verify fail", () => {
61
+ const wrapper = new SHAWrapper();
62
+ const tohashed: string = "This is my array to hash";
63
+ const encoder = new TextEncoder();
64
+ const tohashBytes: Array<number> = Array.from(encoder.encode(tohashed));
65
+ const hashed = wrapper.hash_256(tohashBytes);
66
+ const toVerify = "This Is Not The Same";
67
+ const toVerifyBytes: Array<number> = Array.from(encoder.encode(toVerify));
68
+ const verified = wrapper.verify_256(hashed, toVerifyBytes);
69
+ assert.equal(false, verified);
70
+ });
71
+ });