cas-typescript-sdk 1.0.23 → 1.0.24
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 +3 -17
- package/index.d.ts +22 -21
- package/index.node +0 -0
- package/lib/asymmetric/RSAWrapper.d.ts +3 -3
- package/lib/asymmetric/RSAWrapper.js +1 -1
- package/lib/asymmetric/index.d.ts +2 -2
- package/lib/asymmetric/index.js +1 -3
- package/lib/digital-signature/digital-siganture-sha-512.d.ts +5 -5
- package/lib/digital-signature/digital-siganture-sha-512.js +2 -2
- package/lib/digital-signature/digital-signature-base.d.ts +3 -3
- package/lib/digital-signature/digital-signaturte-sha-256.d.ts +3 -3
- package/lib/digital-signature/index.d.ts +2 -1
- package/lib/hybrid/types/aes-rsa-hybrid-initializer.d.ts +2 -2
- package/lib/index.d.ts +8 -9
- package/lib/index.js +22 -29
- package/lib/key_exchange/index.d.ts +2 -1
- package/lib/key_exchange/x25519.d.ts +2 -2
- package/lib/symmetric/aes-wrapper.d.ts +3 -3
- package/lib/symmetric/index.d.ts +2 -1
- package/package.json +1 -1
- package/src/asymmetric/cas_rsa.rs +13 -59
- package/src/digital_signature/sha_256_ed25519.rs +6 -42
- package/src/digital_signature/sha_256_rsa.rs +7 -63
- package/src/digital_signature/sha_512_ed25519.rs +8 -48
- package/src/digital_signature/sha_512_rsa.rs +9 -62
- package/src/digital_signature/types.rs +34 -0
- package/src/hashers/sha.rs +1 -35
- package/src/key_exchange/types.rs +17 -0
- package/src/key_exchange/x25519.rs +4 -36
- package/src/lib.rs +3 -8
- package/src/password_hashers/argon2.rs +4 -44
- package/src/password_hashers/bcrypt.rs +4 -31
- package/src/password_hashers/scrypt.rs +3 -42
- package/src/sponges/ascon_aead.rs +9 -39
- package/src/symmetric/aes.rs +12 -88
- package/src/symmetric/types.rs +17 -0
- package/src-ts/asymmetric/RSAWrapper.ts +3 -3
- package/src-ts/asymmetric/index.ts +2 -2
- package/src-ts/digital-signature/digital-siganture-sha-512.ts +5 -5
- package/src-ts/digital-signature/digital-signature-base.ts +3 -3
- package/src-ts/digital-signature/digital-signaturte-sha-256.ts +3 -3
- package/src-ts/digital-signature/index.ts +4 -1
- package/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts +2 -2
- package/src-ts/index.ts +8 -46
- package/src-ts/key_exchange/index.ts +2 -1
- package/src-ts/key_exchange/x25519.ts +2 -2
- package/src-ts/symmetric/aes-wrapper.ts +3 -3
- package/src-ts/symmetric/index.ts +2 -1
- package/test-ts/asymmetric.test.spec.ts +3 -3
- package/test-ts/digital-signature.test.spec.ts +5 -5
- package/test-ts/insecure-channel.test.spec.ts +5 -5
- package/src/asymmetric/cas_asymmetric_encryption.rs +0 -15
- package/src/digital_signature/cas_digital_signature_rsa.rs +0 -27
- package/src/hashers/blake2.rs +0 -37
- package/src/hashers/cas_hasher.rs +0 -8
- package/src/key_exchange/cas_key_exchange.rs +0 -6
- package/src/password_hashers/cas_password_hasher.rs +0 -4
- package/src/sponges/cas_ascon_aead.rs +0 -6
- package/src/symmetric/cas_symmetric_encryption.rs +0 -14
|
@@ -1,70 +1,14 @@
|
|
|
1
|
+
use cas_lib::digital_signature::{cas_digital_signature_rsa::RSADigitalSignature, sha_256_rsa::SHA256RSADigitalSignature};
|
|
1
2
|
use napi_derive::napi;
|
|
2
|
-
use rand::rngs::OsRng;
|
|
3
|
-
use rsa::{
|
|
4
|
-
pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey},
|
|
5
|
-
pkcs8::EncodePrivateKey,
|
|
6
|
-
Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey,
|
|
7
|
-
};
|
|
8
|
-
use sha3::{Digest, Sha3_256};
|
|
9
3
|
|
|
10
|
-
|
|
11
|
-
use super::cas_digital_signature_rsa::{RSADigitalSignatureResult, RSADigitalSignature};
|
|
12
|
-
|
|
13
|
-
pub struct SHA256RSADigitalSignature;
|
|
14
|
-
|
|
15
|
-
impl RSADigitalSignature for SHA256RSADigitalSignature {
|
|
16
|
-
fn digital_signature_rsa(
|
|
17
|
-
rsa_key_size: u32,
|
|
18
|
-
data_to_sign: Vec<u8>,
|
|
19
|
-
) -> RSADigitalSignatureResult {
|
|
20
|
-
let mut hasher = Sha3_256::new();
|
|
21
|
-
hasher.update(data_to_sign);
|
|
22
|
-
let sha_hasher_result = hasher.finalize();
|
|
23
|
-
let mut rng: OsRng = OsRng;
|
|
24
|
-
let private_key: RsaPrivateKey =
|
|
25
|
-
RsaPrivateKey::new(&mut rng, rsa_key_size as usize).expect("failed to generate a key");
|
|
26
|
-
let public_key = private_key.to_public_key();
|
|
27
|
-
let mut signed_data = private_key
|
|
28
|
-
.sign(Pkcs1v15Sign::new_unprefixed(), &sha_hasher_result)
|
|
29
|
-
.unwrap();
|
|
30
|
-
let result = RSADigitalSignatureResult {
|
|
31
|
-
private_key: private_key
|
|
32
|
-
.to_pkcs8_pem(rsa::pkcs8::LineEnding::LF)
|
|
33
|
-
.unwrap()
|
|
34
|
-
.to_string(),
|
|
35
|
-
public_key: public_key
|
|
36
|
-
.to_pkcs1_pem(rsa::pkcs8::LineEnding::LF)
|
|
37
|
-
.unwrap()
|
|
38
|
-
.to_string(),
|
|
39
|
-
signature: signed_data,
|
|
40
|
-
};
|
|
41
|
-
result
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
fn verify_rsa(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
|
|
45
|
-
let mut hasher = Sha3_256::new();
|
|
46
|
-
hasher.update(data_to_verify);
|
|
47
|
-
let sha_hasher_result = hasher.finalize();
|
|
48
|
-
let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap();
|
|
49
|
-
let verified = public_key.verify(
|
|
50
|
-
Pkcs1v15Sign::new_unprefixed(),
|
|
51
|
-
&sha_hasher_result,
|
|
52
|
-
&signature,
|
|
53
|
-
);
|
|
54
|
-
if verified.is_err() == false {
|
|
55
|
-
return true;
|
|
56
|
-
} else {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
4
|
+
use super::types::CASRSADigitalSignatureResult;
|
|
61
5
|
|
|
62
6
|
#[napi]
|
|
63
7
|
pub fn sha_256_rsa_digital_signature(
|
|
64
8
|
rsa_key_size: u32,
|
|
65
9
|
data_to_sign: Vec<u8>,
|
|
66
|
-
) ->
|
|
67
|
-
return SHA256RSADigitalSignature
|
|
10
|
+
) -> CASRSADigitalSignatureResult {
|
|
11
|
+
return <SHA256RSADigitalSignature as RSADigitalSignature>::digital_signature_rsa(rsa_key_size, data_to_sign).into();
|
|
68
12
|
}
|
|
69
13
|
|
|
70
14
|
#[napi]
|
|
@@ -73,14 +17,14 @@ pub fn sha_256_rsa_verify_digital_signature(
|
|
|
73
17
|
data_to_verify: Vec<u8>,
|
|
74
18
|
signature: Vec<u8>,
|
|
75
19
|
) -> bool {
|
|
76
|
-
return SHA256RSADigitalSignature
|
|
20
|
+
return <SHA256RSADigitalSignature as RSADigitalSignature>::verify_rsa(public_key, data_to_verify, signature);
|
|
77
21
|
}
|
|
78
22
|
|
|
79
23
|
#[test]
|
|
80
24
|
fn sha_256_rsa_digital_signature_test() {
|
|
81
25
|
let key_size: u32 = 1024;
|
|
82
26
|
let data_to_sign = b"GetTheseBytes".to_vec();
|
|
83
|
-
let signature_result:
|
|
27
|
+
let signature_result: CASRSADigitalSignatureResult = <SHA256RSADigitalSignature as RSADigitalSignature>::digital_signature_rsa(key_size, data_to_sign.clone()).into();
|
|
84
28
|
let is_verified: bool = SHA256RSADigitalSignature::verify_rsa(signature_result.public_key, data_to_sign, signature_result.signature);
|
|
85
29
|
assert_eq!(is_verified, true);
|
|
86
30
|
}
|
|
@@ -89,7 +33,7 @@ fn sha_256_rsa_digital_signature_test() {
|
|
|
89
33
|
fn sha_256_rsa_digital_signature_fail_test() {
|
|
90
34
|
let key_size: u32 = 1024;
|
|
91
35
|
let data_to_sign = b"GetTheseBytes".to_vec();
|
|
92
|
-
let signature_result:
|
|
36
|
+
let signature_result: CASRSADigitalSignatureResult = <SHA256RSADigitalSignature as RSADigitalSignature>::digital_signature_rsa(key_size, data_to_sign.clone()).into();
|
|
93
37
|
let new_data = b"NOtTheOriginalData".to_vec();
|
|
94
38
|
let is_verified: bool = SHA256RSADigitalSignature::verify_rsa(signature_result.public_key, new_data, signature_result.signature);
|
|
95
39
|
assert_eq!(is_verified, false);
|
|
@@ -1,65 +1,25 @@
|
|
|
1
|
-
use
|
|
2
|
-
use napi_derive::napi;
|
|
3
|
-
use sha3::{Digest, Sha3_512};
|
|
4
|
-
|
|
5
|
-
use super::cas_digital_signature_rsa::{
|
|
6
|
-
ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult,
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
pub struct SHA512ED25519DigitalSignature;
|
|
1
|
+
use cas_lib::digital_signature::{cas_digital_signature_rsa::{ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult}, sha_512_ed25519::SHA512ED25519DigitalSignature};
|
|
10
2
|
|
|
11
|
-
|
|
12
|
-
fn digital_signature_ed25519(
|
|
13
|
-
data_to_sign: Vec<u8>,
|
|
14
|
-
) -> SHAED25519DalekDigitalSignatureResult {
|
|
15
|
-
let mut hasher = Sha3_512::new();
|
|
16
|
-
hasher.update(data_to_sign);
|
|
17
|
-
let sha_hasher_result = hasher.finalize();
|
|
18
|
-
let mut csprng = rand_07::rngs::OsRng {};
|
|
19
|
-
let keypair = Keypair::generate(&mut csprng);
|
|
20
|
-
|
|
21
|
-
let signature = keypair.sign(&sha_hasher_result);
|
|
22
|
-
let signature_bytes = signature.to_bytes();
|
|
23
|
-
let public_keypair_bytes = keypair.public.to_bytes();
|
|
24
|
-
let result = SHAED25519DalekDigitalSignatureResult {
|
|
25
|
-
public_key: public_keypair_bytes.to_vec(),
|
|
26
|
-
signature: signature_bytes.to_vec(),
|
|
27
|
-
};
|
|
28
|
-
result
|
|
29
|
-
}
|
|
3
|
+
use napi_derive::napi;
|
|
30
4
|
|
|
31
|
-
|
|
32
|
-
public_key: Vec<u8>,
|
|
33
|
-
data_to_verify: Vec<u8>,
|
|
34
|
-
signature: Vec<u8>,
|
|
35
|
-
) -> bool {
|
|
36
|
-
let mut hasher = Sha3_512::new();
|
|
37
|
-
hasher.update(data_to_verify);
|
|
38
|
-
let sha_hasher_result = hasher.finalize();
|
|
5
|
+
use super::types::CASSHAED25519DalekDigitalSignatureResult;
|
|
39
6
|
|
|
40
|
-
let public_key_parsed = ed25519_dalek::PublicKey::from_bytes(&public_key).unwrap();
|
|
41
|
-
let signature_parsed = Signature::from_bytes(&signature).unwrap();
|
|
42
|
-
return public_key_parsed
|
|
43
|
-
.verify(&sha_hasher_result, &signature_parsed)
|
|
44
|
-
.is_ok();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
7
|
|
|
48
8
|
#[napi]
|
|
49
|
-
pub fn sha_512_ed25519_digital_signature(data_to_sign: Vec<u8>) ->
|
|
50
|
-
return SHA512ED25519DigitalSignature
|
|
9
|
+
pub fn sha_512_ed25519_digital_signature(data_to_sign: Vec<u8>) -> CASSHAED25519DalekDigitalSignatureResult {
|
|
10
|
+
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign).into();
|
|
51
11
|
}
|
|
52
12
|
|
|
53
13
|
#[napi]
|
|
54
14
|
pub fn sha_512_ed25519_digital_signature_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
|
|
55
|
-
return SHA512ED25519DigitalSignature
|
|
15
|
+
return <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(public_key, data_to_verify, signature)
|
|
56
16
|
}
|
|
57
17
|
|
|
58
18
|
#[test]
|
|
59
19
|
fn sha_512_ed25519_test() {
|
|
60
20
|
let key_size: u32 = 1024;
|
|
61
21
|
let data_to_sign = b"GetTheseBytes".to_vec();
|
|
62
|
-
let signature_result: SHAED25519DalekDigitalSignatureResult = SHA512ED25519DigitalSignature
|
|
22
|
+
let signature_result: SHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign.clone());
|
|
63
23
|
let is_verified: bool = SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, data_to_sign, signature_result.signature);
|
|
64
24
|
assert_eq!(is_verified, true);
|
|
65
25
|
}
|
|
@@ -68,7 +28,7 @@ fn sha_512_ed25519_test() {
|
|
|
68
28
|
fn sha_512_ed25519_test_fail() {
|
|
69
29
|
let key_size: u32 = 1024;
|
|
70
30
|
let data_to_sign = b"GetTheseBytes".to_vec();
|
|
71
|
-
let signature_result:
|
|
31
|
+
let signature_result: CASSHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign.clone()).into();
|
|
72
32
|
let not_original_data = b"NOtTHoseBytes".to_vec();
|
|
73
33
|
let is_verified: bool = SHA512ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, not_original_data, signature_result.signature);
|
|
74
34
|
assert_eq!(is_verified, false);
|
|
@@ -1,67 +1,14 @@
|
|
|
1
|
+
use cas_lib::digital_signature::{cas_digital_signature_rsa::RSADigitalSignature, sha_512_rsa::SHA512RSADigitalSignature};
|
|
1
2
|
use napi_derive::napi;
|
|
2
|
-
use rand::rngs::OsRng;
|
|
3
|
-
use rsa::{
|
|
4
|
-
pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey},
|
|
5
|
-
pkcs8::EncodePrivateKey,
|
|
6
|
-
Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey,
|
|
7
|
-
};
|
|
8
|
-
use sha3::{Digest, Sha3_512};
|
|
9
|
-
use super::cas_digital_signature_rsa::{RSADigitalSignatureResult, RSADigitalSignature};
|
|
10
|
-
pub struct SHA512RSADigitalSignature;
|
|
11
3
|
|
|
12
|
-
|
|
13
|
-
fn digital_signature_rsa(
|
|
14
|
-
rsa_key_size: u32,
|
|
15
|
-
data_to_sign: Vec<u8>,
|
|
16
|
-
) -> RSADigitalSignatureResult {
|
|
17
|
-
let mut hasher = Sha3_512::new();
|
|
18
|
-
hasher.update(data_to_sign);
|
|
19
|
-
let sha_hasher_result = hasher.finalize();
|
|
20
|
-
let mut rng: OsRng = OsRng;
|
|
21
|
-
let private_key: RsaPrivateKey =
|
|
22
|
-
RsaPrivateKey::new(&mut rng, rsa_key_size as usize).expect("failed to generate a key");
|
|
23
|
-
let public_key = private_key.to_public_key();
|
|
24
|
-
let mut signed_data = private_key
|
|
25
|
-
.sign(Pkcs1v15Sign::new_unprefixed(), &sha_hasher_result)
|
|
26
|
-
.unwrap();
|
|
27
|
-
let result = RSADigitalSignatureResult {
|
|
28
|
-
private_key: private_key
|
|
29
|
-
.to_pkcs8_pem(rsa::pkcs8::LineEnding::LF)
|
|
30
|
-
.unwrap()
|
|
31
|
-
.to_string(),
|
|
32
|
-
public_key: public_key
|
|
33
|
-
.to_pkcs1_pem(rsa::pkcs8::LineEnding::LF)
|
|
34
|
-
.unwrap()
|
|
35
|
-
.to_string(),
|
|
36
|
-
signature: signed_data,
|
|
37
|
-
};
|
|
38
|
-
result
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
fn verify_rsa(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool {
|
|
42
|
-
let mut hasher = Sha3_512::new();
|
|
43
|
-
hasher.update(data_to_verify);
|
|
44
|
-
let sha_hasher_result = hasher.finalize();
|
|
45
|
-
let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap();
|
|
46
|
-
let verified = public_key.verify(
|
|
47
|
-
Pkcs1v15Sign::new_unprefixed(),
|
|
48
|
-
&sha_hasher_result,
|
|
49
|
-
&signature,
|
|
50
|
-
);
|
|
51
|
-
if verified.is_err() == false {
|
|
52
|
-
return true;
|
|
53
|
-
} else {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
4
|
+
use super::types::CASRSADigitalSignatureResult;
|
|
58
5
|
|
|
59
6
|
#[napi]
|
|
60
7
|
pub fn sha_512_rsa_digital_signature(
|
|
61
8
|
rsa_key_size: u32,
|
|
62
9
|
data_to_sign: Vec<u8>,
|
|
63
|
-
) ->
|
|
64
|
-
return SHA512RSADigitalSignature
|
|
10
|
+
) -> CASRSADigitalSignatureResult {
|
|
11
|
+
return <SHA512RSADigitalSignature as RSADigitalSignature>::digital_signature_rsa(rsa_key_size, data_to_sign).into();
|
|
65
12
|
}
|
|
66
13
|
|
|
67
14
|
#[napi]
|
|
@@ -70,15 +17,15 @@ pub fn sha_512_rsa_verify_digital_signature(
|
|
|
70
17
|
data_to_verify: Vec<u8>,
|
|
71
18
|
signature: Vec<u8>,
|
|
72
19
|
) -> bool {
|
|
73
|
-
return SHA512RSADigitalSignature
|
|
20
|
+
return <SHA512RSADigitalSignature as RSADigitalSignature>::verify_rsa(public_key, data_to_verify, signature);
|
|
74
21
|
}
|
|
75
22
|
|
|
76
23
|
#[test]
|
|
77
24
|
fn sha_512_rsa_digital_signature_test() {
|
|
78
25
|
let key_size: u32 = 1024;
|
|
79
26
|
let data_to_sign = b"GetTheseBytes".to_vec();
|
|
80
|
-
let signature_result:
|
|
81
|
-
let is_verified: bool = SHA512RSADigitalSignature
|
|
27
|
+
let signature_result: CASRSADigitalSignatureResult = SHA512RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()).into();
|
|
28
|
+
let is_verified: bool = <SHA512RSADigitalSignature as RSADigitalSignature>::verify_rsa(signature_result.public_key, data_to_sign, signature_result.signature);
|
|
82
29
|
assert_eq!(is_verified, true);
|
|
83
30
|
}
|
|
84
31
|
|
|
@@ -86,8 +33,8 @@ fn sha_512_rsa_digital_signature_test() {
|
|
|
86
33
|
fn sha_512_rsa_digital_signature_fail_test() {
|
|
87
34
|
let key_size: u32 = 1024;
|
|
88
35
|
let data_to_sign = b"GetTheseBytes".to_vec();
|
|
89
|
-
let signature_result:
|
|
36
|
+
let signature_result: CASRSADigitalSignatureResult = SHA512RSADigitalSignature::digital_signature_rsa(key_size, data_to_sign.clone()).into();
|
|
90
37
|
let new_data = b"NOtTheOriginalData".to_vec();
|
|
91
|
-
let is_verified: bool = SHA512RSADigitalSignature
|
|
38
|
+
let is_verified: bool = <SHA512RSADigitalSignature as RSADigitalSignature>::verify_rsa(signature_result.public_key, new_data, signature_result.signature);
|
|
92
39
|
assert_eq!(is_verified, false);
|
|
93
40
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
use cas_lib::digital_signature::cas_digital_signature_rsa::{RSADigitalSignatureResult, SHAED25519DalekDigitalSignatureResult};
|
|
2
|
+
use napi_derive::napi;
|
|
3
|
+
|
|
4
|
+
#[napi(constructor)]
|
|
5
|
+
pub struct CASSHAED25519DalekDigitalSignatureResult {
|
|
6
|
+
pub public_key: Vec<u8>,
|
|
7
|
+
pub signature: Vec<u8>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl From<SHAED25519DalekDigitalSignatureResult> for CASSHAED25519DalekDigitalSignatureResult {
|
|
11
|
+
fn from(value: SHAED25519DalekDigitalSignatureResult) -> Self {
|
|
12
|
+
CASSHAED25519DalekDigitalSignatureResult {
|
|
13
|
+
public_key: value.public_key,
|
|
14
|
+
signature: value.signature
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#[napi(constructor)]
|
|
20
|
+
pub struct CASRSADigitalSignatureResult {
|
|
21
|
+
pub public_key: String,
|
|
22
|
+
pub private_key: String,
|
|
23
|
+
pub signature: Vec<u8>,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
impl From<RSADigitalSignatureResult> for CASRSADigitalSignatureResult {
|
|
27
|
+
fn from(value: RSADigitalSignatureResult) -> Self {
|
|
28
|
+
CASRSADigitalSignatureResult {
|
|
29
|
+
public_key: value.public_key,
|
|
30
|
+
private_key: value.private_key,
|
|
31
|
+
signature: value.signature
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/hashers/sha.rs
CHANGED
|
@@ -1,40 +1,6 @@
|
|
|
1
|
-
use
|
|
1
|
+
use cas_lib::hashers::{cas_hasher::CASHasher, sha::CASSHA};
|
|
2
2
|
use napi_derive::napi;
|
|
3
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
4
|
#[napi]
|
|
39
5
|
pub fn sha_512(data_to_hash: Vec<u8>) -> Vec<u8> {
|
|
40
6
|
return <CASSHA as CASHasher>::hash_512(data_to_hash);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
use cas_lib::key_exchange::x25519::x25519SecretPublicKeyResult;
|
|
2
|
+
use napi_derive::napi;
|
|
3
|
+
|
|
4
|
+
#[napi(constructor)]
|
|
5
|
+
pub struct CASx25519SecretPublicKeyResult {
|
|
6
|
+
pub public_key: Vec<u8>,
|
|
7
|
+
pub secret_key: Vec<u8>,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl From<x25519SecretPublicKeyResult> for CASx25519SecretPublicKeyResult {
|
|
11
|
+
fn from(value: x25519SecretPublicKeyResult) -> Self {
|
|
12
|
+
CASx25519SecretPublicKeyResult {
|
|
13
|
+
public_key: value.public_key,
|
|
14
|
+
secret_key: value.secret_key
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,44 +1,12 @@
|
|
|
1
|
-
use
|
|
1
|
+
use cas_lib::key_exchange::{cas_key_exchange::CASKeyExchange, x25519::X25519};
|
|
2
2
|
use napi_derive::napi;
|
|
3
|
-
use rand::rngs::OsRng;
|
|
4
|
-
use x25519_dalek::{PublicKey, StaticSecret};
|
|
5
3
|
|
|
6
|
-
use super::
|
|
4
|
+
use super::types::CASx25519SecretPublicKeyResult;
|
|
7
5
|
|
|
8
|
-
#[napi(constructor)]
|
|
9
|
-
pub struct x25519SecretPublicKeyResult {
|
|
10
|
-
pub public_key: Vec<u8>,
|
|
11
|
-
pub secret_key: Vec<u8>,
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
pub struct X25519;
|
|
15
|
-
|
|
16
|
-
impl CASKeyExchange for X25519 {
|
|
17
|
-
fn generate_secret_and_public_key() -> x25519SecretPublicKeyResult {
|
|
18
|
-
let secret_key = StaticSecret::random_from_rng(OsRng);
|
|
19
|
-
let public_key = PublicKey::from(&secret_key);
|
|
20
|
-
let result = x25519SecretPublicKeyResult {
|
|
21
|
-
secret_key: secret_key.as_bytes().to_vec(),
|
|
22
|
-
public_key: public_key.as_bytes().to_vec(),
|
|
23
|
-
};
|
|
24
|
-
result
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
fn diffie_hellman(my_secret_key: Vec<u8>, users_public_key: Vec<u8>) -> Vec<u8> {
|
|
28
|
-
let mut secret_key_array: [u8; 32] = Default::default();
|
|
29
|
-
secret_key_array.copy_from_slice(&my_secret_key);
|
|
30
|
-
let mut users_public_key_array: [u8; 32] = Default::default();
|
|
31
|
-
users_public_key_array.copy_from_slice(&users_public_key);
|
|
32
|
-
|
|
33
|
-
let secret_key = StaticSecret::from(secret_key_array);
|
|
34
|
-
let public_key = PublicKey::from(users_public_key_array);
|
|
35
|
-
return secret_key.diffie_hellman(&public_key).as_bytes().to_vec();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
6
|
|
|
39
7
|
#[napi]
|
|
40
|
-
pub fn x25519_generate_secret_and_public_key() ->
|
|
41
|
-
return <X25519 as CASKeyExchange>::generate_secret_and_public_key();
|
|
8
|
+
pub fn x25519_generate_secret_and_public_key() -> CASx25519SecretPublicKeyResult {
|
|
9
|
+
return <X25519 as CASKeyExchange>::generate_secret_and_public_key().into();
|
|
42
10
|
}
|
|
43
11
|
|
|
44
12
|
#[napi]
|
package/src/lib.rs
CHANGED
|
@@ -1,40 +1,35 @@
|
|
|
1
1
|
mod password_hashers {
|
|
2
2
|
pub mod argon2;
|
|
3
3
|
pub mod bcrypt;
|
|
4
|
-
pub mod cas_password_hasher;
|
|
5
4
|
pub mod scrypt;
|
|
6
5
|
}
|
|
7
6
|
|
|
8
7
|
mod hashers {
|
|
9
|
-
pub mod blake2;
|
|
10
|
-
pub mod cas_hasher;
|
|
11
8
|
pub mod sha;
|
|
12
9
|
}
|
|
13
10
|
|
|
14
11
|
mod key_exchange {
|
|
15
|
-
pub mod cas_key_exchange;
|
|
16
12
|
pub mod x25519;
|
|
13
|
+
mod types;
|
|
17
14
|
}
|
|
18
15
|
|
|
19
16
|
mod symmetric {
|
|
20
17
|
pub mod aes;
|
|
21
|
-
|
|
18
|
+
mod types;
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
mod asymmetric {
|
|
25
|
-
pub mod cas_asymmetric_encryption;
|
|
26
22
|
pub mod cas_rsa;
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
mod digital_signature {
|
|
30
|
-
pub mod cas_digital_signature_rsa;
|
|
31
26
|
pub mod sha_512_rsa;
|
|
32
27
|
pub mod sha_256_rsa;
|
|
33
28
|
pub mod sha_512_ed25519;
|
|
34
29
|
pub mod sha_256_ed25519;
|
|
30
|
+
mod types;
|
|
35
31
|
}
|
|
36
32
|
|
|
37
33
|
mod sponges {
|
|
38
|
-
pub mod cas_ascon_aead;
|
|
39
34
|
pub mod ascon_aead;
|
|
40
35
|
}
|
|
@@ -1,35 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
use napi_derive::napi;
|
|
3
|
-
|
|
4
|
-
use
|
|
5
|
-
password_hash::{rand_core::OsRng, SaltString},
|
|
6
|
-
Argon2, PasswordHash, PasswordHasher, PasswordVerifier,
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
use crate::symmetric::aes::CASAES128;
|
|
10
|
-
|
|
11
|
-
use super::cas_password_hasher::CASPasswordHasher;
|
|
12
|
-
|
|
13
|
-
pub struct CASArgon;
|
|
14
|
-
|
|
15
|
-
impl CASPasswordHasher for CASArgon {
|
|
16
|
-
fn hash_password(password_to_hash: String) -> String {
|
|
17
|
-
let salt = SaltString::generate(&mut OsRng);
|
|
18
|
-
let argon2 = Argon2::default();
|
|
19
|
-
let hashed_password = argon2
|
|
20
|
-
.hash_password(password_to_hash.as_bytes(), &salt)
|
|
21
|
-
.unwrap()
|
|
22
|
-
.to_string();
|
|
23
|
-
return hashed_password;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
|
|
27
|
-
let hashed_password = PasswordHash::new(&hashed_password).unwrap();
|
|
28
|
-
return Argon2::default()
|
|
29
|
-
.verify_password(password_to_verify.as_bytes(), &hashed_password)
|
|
30
|
-
.is_ok();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
3
|
+
use cas_lib::password_hashers::argon2::CASArgon;
|
|
4
|
+
use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher;
|
|
33
5
|
|
|
34
6
|
#[napi]
|
|
35
7
|
pub fn argon2_hash(password: String) -> String {
|
|
@@ -38,13 +10,7 @@ pub fn argon2_hash(password: String) -> String {
|
|
|
38
10
|
|
|
39
11
|
#[napi]
|
|
40
12
|
pub fn argon2_hash_thread_pool(password: String) -> String {
|
|
41
|
-
|
|
42
|
-
rayon::spawn(move || {
|
|
43
|
-
let hash_result = <CASArgon as CASPasswordHasher>::hash_password(password);
|
|
44
|
-
sender.send(hash_result);
|
|
45
|
-
});
|
|
46
|
-
let result = receiver.recv().unwrap();
|
|
47
|
-
result
|
|
13
|
+
return <CASArgon as CASPasswordHasher>::hash__password_threadpool(password);
|
|
48
14
|
}
|
|
49
15
|
|
|
50
16
|
#[napi]
|
|
@@ -54,13 +20,7 @@ pub fn argon2_verify(hashed_password: String, password_to_verify: String) -> boo
|
|
|
54
20
|
|
|
55
21
|
#[napi]
|
|
56
22
|
pub fn argon2_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool {
|
|
57
|
-
|
|
58
|
-
rayon::spawn(move || {
|
|
59
|
-
let verify_result = <CASArgon as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
|
|
60
|
-
sender.send(verify_result);
|
|
61
|
-
});
|
|
62
|
-
let result = receiver.recv().unwrap();
|
|
63
|
-
result
|
|
23
|
+
return <CASArgon as CASPasswordHasher>::verify_password_threadpool(hashed_password, password_to_verify);
|
|
64
24
|
}
|
|
65
25
|
|
|
66
26
|
#[test]
|
|
@@ -1,22 +1,7 @@
|
|
|
1
|
-
use
|
|
2
|
-
|
|
3
|
-
use bcrypt::{hash, verify, DEFAULT_COST};
|
|
1
|
+
use cas_lib::password_hashers::bcrypt::CASBCrypt;
|
|
2
|
+
use cas_lib::password_hashers::cas_password_hasher::CASPasswordHasher;
|
|
4
3
|
use napi_derive::napi;
|
|
5
4
|
|
|
6
|
-
use super::cas_password_hasher::CASPasswordHasher;
|
|
7
|
-
|
|
8
|
-
pub struct CASBCrypt;
|
|
9
|
-
|
|
10
|
-
impl CASPasswordHasher for CASBCrypt {
|
|
11
|
-
fn hash_password(password_to_hash: String) -> String {
|
|
12
|
-
return hash(password_to_hash, DEFAULT_COST).unwrap();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
|
|
16
|
-
return verify(password_to_verify, &hashed_password).unwrap();
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
5
|
#[napi]
|
|
21
6
|
pub fn bcrypt_hash(password_to_hash: String) -> String {
|
|
22
7
|
return <CASBCrypt as CASPasswordHasher>::hash_password(password_to_hash);
|
|
@@ -24,13 +9,7 @@ pub fn bcrypt_hash(password_to_hash: String) -> String {
|
|
|
24
9
|
|
|
25
10
|
#[napi]
|
|
26
11
|
pub fn bcrypt_hash_threadpool(password_to_hash: String) -> String {
|
|
27
|
-
|
|
28
|
-
rayon::spawn(move || {
|
|
29
|
-
let thread_result = <CASBCrypt as CASPasswordHasher>::hash_password(password_to_hash);
|
|
30
|
-
sender.send(thread_result);
|
|
31
|
-
});
|
|
32
|
-
let result = receiver.recv().unwrap();
|
|
33
|
-
result
|
|
12
|
+
return <CASBCrypt as CASPasswordHasher>::hash__password_threadpool(password_to_hash);
|
|
34
13
|
}
|
|
35
14
|
|
|
36
15
|
#[napi]
|
|
@@ -40,13 +19,7 @@ pub fn bcrypt_verify(hashed_password: String, password_to_verify: String) -> boo
|
|
|
40
19
|
|
|
41
20
|
#[napi]
|
|
42
21
|
pub fn bcrypt_verify_threadpool(password_to_hash: String, password_to_verify: String) -> bool {
|
|
43
|
-
|
|
44
|
-
rayon::spawn(move || {
|
|
45
|
-
let thread_result = <CASBCrypt as CASPasswordHasher>::verify_password(password_to_hash, password_to_verify);
|
|
46
|
-
sender.send(thread_result);
|
|
47
|
-
});
|
|
48
|
-
let result = receiver.recv().unwrap();
|
|
49
|
-
result
|
|
22
|
+
return <CASBCrypt as CASPasswordHasher>::verify_password_threadpool(password_to_hash, password_to_verify);
|
|
50
23
|
}
|
|
51
24
|
|
|
52
25
|
#[test]
|
|
@@ -1,33 +1,6 @@
|
|
|
1
|
-
use
|
|
2
|
-
|
|
1
|
+
use cas_lib::password_hashers::{cas_password_hasher::CASPasswordHasher, scrypt::CASScrypt};
|
|
3
2
|
use napi_derive::napi;
|
|
4
3
|
|
|
5
|
-
use scrypt::{
|
|
6
|
-
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
|
7
|
-
Scrypt,
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
use super::cas_password_hasher::CASPasswordHasher;
|
|
11
|
-
|
|
12
|
-
pub struct CASScrypt;
|
|
13
|
-
|
|
14
|
-
impl CASPasswordHasher for CASScrypt {
|
|
15
|
-
fn hash_password(password_to_hash: String) -> String {
|
|
16
|
-
let salt = SaltString::generate(&mut OsRng);
|
|
17
|
-
return Scrypt
|
|
18
|
-
.hash_password(password_to_hash.as_bytes(), &salt)
|
|
19
|
-
.unwrap()
|
|
20
|
-
.to_string();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
|
|
24
|
-
let parsed_hash = PasswordHash::new(&hashed_password).unwrap();
|
|
25
|
-
return Scrypt
|
|
26
|
-
.verify_password(password_to_verify.as_bytes(), &parsed_hash)
|
|
27
|
-
.is_ok();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
4
|
#[napi]
|
|
32
5
|
pub fn scrypt_hash(password_to_hash: String) -> String {
|
|
33
6
|
return <CASScrypt as CASPasswordHasher>::hash_password(password_to_hash);
|
|
@@ -40,24 +13,12 @@ pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> boo
|
|
|
40
13
|
|
|
41
14
|
#[napi]
|
|
42
15
|
pub fn scrypt_hash_threadpool(password_to_hash: String) -> String {
|
|
43
|
-
|
|
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
|
|
16
|
+
return <CASScrypt as CASPasswordHasher>::hash__password_threadpool(password_to_hash);
|
|
50
17
|
}
|
|
51
18
|
|
|
52
19
|
#[napi]
|
|
53
20
|
pub fn scrypt_verify_threadpool(hashed_password: String, password_to_verify: String) -> bool {
|
|
54
|
-
|
|
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
|
|
21
|
+
return <CASScrypt as CASPasswordHasher>::verify_password_threadpool(hashed_password, password_to_verify);
|
|
61
22
|
}
|
|
62
23
|
|
|
63
24
|
#[test]
|