cas-typescript-sdk 1.0.14 → 1.0.16

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.
Files changed (95) hide show
  1. package/.github/workflows/main-pr-linux.yml +28 -0
  2. package/.github/workflows/main-pr-windows.yml +28 -0
  3. package/.github/workflows/main-publish.yml +32 -0
  4. package/Cargo.toml +8 -1
  5. package/README.md +3 -1
  6. package/build.rs +5 -5
  7. package/docs/EXAMPLES.md +39 -0
  8. package/index.d.ts +19 -0
  9. package/index.node +0 -0
  10. package/lib/digital-signature/digital-siganture-sha-512.d.ts +6 -0
  11. package/lib/digital-signature/digital-siganture-sha-512.js +28 -0
  12. package/lib/digital-signature/digital-signature-base.d.ts +5 -0
  13. package/lib/digital-signature/digital-signature-factory.d.ts +8 -0
  14. package/lib/digital-signature/digital-signature-factory.js +22 -0
  15. package/lib/digital-signature/digital-signaturte-sha-256.d.ts +6 -0
  16. package/lib/digital-signature/digital-signaturte-sha-256.js +28 -0
  17. package/lib/digital-signature/index.d.ts +4 -0
  18. package/lib/digital-signature/index.js +8 -0
  19. package/lib/hybrid/hybrid-encryption-wrapper.d.ts +9 -0
  20. package/lib/hybrid/hybrid-encryption-wrapper.js +30 -0
  21. package/lib/hybrid/index.d.ts +4 -0
  22. package/lib/hybrid/index.js +9 -0
  23. package/lib/hybrid/types/aes-rsa-hybird-encrypt-result.d.ts +7 -0
  24. package/lib/hybrid/types/aes-rsa-hybird-encrypt-result.js +16 -0
  25. package/lib/hybrid/types/aes-rsa-hybrid-initializer.d.ts +8 -0
  26. package/lib/hybrid/types/aes-rsa-hybrid-initializer.js +25 -0
  27. package/lib/index.d.ts +3 -2
  28. package/lib/index.js +8 -4
  29. package/package.json +41 -39
  30. package/src/asymmetric/cas_asymmetric_encryption.rs +15 -15
  31. package/src/asymmetric/cas_rsa.rs +88 -80
  32. package/src/digital_signature/cas_digital_signature_rsa.rs +27 -0
  33. package/src/digital_signature/sha_256_rsa.rs +96 -0
  34. package/src/digital_signature/sha_512_ed25519.rs +75 -0
  35. package/src/digital_signature/sha_512_rsa.rs +93 -0
  36. package/src/hashers/blake2.rs +37 -39
  37. package/src/hashers/cas_hasher.rs +8 -8
  38. package/src/hashers/sha.rs +102 -103
  39. package/src/key_exchange/cas_key_exchange.rs +6 -6
  40. package/src/key_exchange/x25519.rs +57 -57
  41. package/src/lib.rs +34 -27
  42. package/src/password_hashers/argon2.rs +65 -64
  43. package/src/password_hashers/bcrypt.rs +50 -51
  44. package/src/password_hashers/cas_password_hasher.rs +4 -4
  45. package/src/password_hashers/scrypt.rs +61 -56
  46. package/src/symmetric/aes.rs +155 -151
  47. package/src/symmetric/cas_symmetric_encryption.rs +14 -14
  48. package/src-ts/asymmetric/RSAWrapper.ts +53 -53
  49. package/src-ts/asymmetric/index.ts +3 -3
  50. package/src-ts/digital-signature/digital-siganture-sha-512.ts +28 -0
  51. package/src-ts/digital-signature/digital-signature-base.ts +6 -0
  52. package/src-ts/digital-signature/digital-signature-factory.ts +19 -0
  53. package/src-ts/digital-signature/digital-signaturte-sha-256.ts +28 -0
  54. package/src-ts/digital-signature/index.ts +4 -0
  55. package/src-ts/global.d.ts +1 -1
  56. package/src-ts/hashers/hasher-base.ts +5 -5
  57. package/src-ts/hashers/hasher-factory.ts +11 -11
  58. package/src-ts/hashers/hasher-type.ts +2 -2
  59. package/src-ts/hashers/index.ts +5 -5
  60. package/src-ts/hashers/sha-wrapper.ts +37 -37
  61. package/src-ts/helpers/nonce-generator.ts +8 -8
  62. package/src-ts/hybrid/hybrid-encryption-wrapper.ts +64 -0
  63. package/src-ts/hybrid/index.ts +9 -0
  64. package/src-ts/hybrid/types/aes-rsa-hybird-encrypt-result.ts +13 -0
  65. package/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts +24 -0
  66. package/src-ts/index.ts +34 -26
  67. package/src-ts/key_exchange/index.ts +3 -3
  68. package/src-ts/key_exchange/x25519.ts +10 -10
  69. package/src-ts/password-hashers/argon2-wrapper.ts +18 -18
  70. package/src-ts/password-hashers/bcrypt-wrapper.ts +23 -23
  71. package/src-ts/password-hashers/index.ts +14 -14
  72. package/src-ts/password-hashers/password-hasher-base.ts +3 -3
  73. package/src-ts/password-hashers/password-hasher-factory.ts +20 -20
  74. package/src-ts/password-hashers/password-hasher-type.ts +4 -4
  75. package/src-ts/password-hashers/scrypt-wrapper.ts +19 -19
  76. package/src-ts/symmetric/aes-wrapper.ts +50 -50
  77. package/src-ts/symmetric/index.ts +3 -3
  78. package/test-ts/asymmetric.test.spec.ts +27 -27
  79. package/test-ts/digital-signature.test.spec.ts +49 -0
  80. package/test-ts/hasher.test.spec.ts +70 -70
  81. package/test-ts/helpers/array.ts +9 -9
  82. package/test-ts/hybrid.test.spec.ts +33 -0
  83. package/test-ts/insecure-channel.test.spec.ts +50 -50
  84. package/test-ts/key-exchange-test.spec.ts +23 -23
  85. package/test-ts/password-hasher-test.spec.ts +102 -102
  86. package/test-ts/symmetric.test.spec.ts +31 -31
  87. package/tsconfig.json +21 -21
  88. package/lib/cas_core_lib.dll +0 -0
  89. package/lib/hashers/IHasherBase.d.ts +0 -6
  90. package/lib/hashers/SHAWrapper.d.ts +0 -7
  91. package/lib/hashers/SHAWrapper.js +0 -37
  92. package/lib/libcas_core_lib.so +0 -0
  93. package/lib/password-hashers/types/argon2-hash-thread-result.d.ts +0 -3
  94. package/lib/password-hashers/types/argon2-hash-thread-result.js +0 -11
  95. /package/lib/{hashers/IHasherBase.js → digital-signature/digital-signature-base.js} +0 -0
@@ -1,56 +1,61 @@
1
- use napi_derive::napi;
2
-
3
- use scrypt::{
4
- password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
5
- Scrypt,
6
- };
7
-
8
- use super::cas_password_hasher::CASPasswordHasher;
9
-
10
- pub struct CASScrypt;
11
-
12
- impl CASPasswordHasher for CASScrypt {
13
- fn hash_password(password_to_hash: String) -> String {
14
- let salt = SaltString::generate(&mut OsRng);
15
- return Scrypt.hash_password(password_to_hash.as_bytes(), &salt).unwrap().to_string();
16
- }
17
-
18
- fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
19
- let parsed_hash = PasswordHash::new(&hashed_password).unwrap();
20
- return Scrypt.verify_password(password_to_verify.as_bytes(), &parsed_hash).is_ok();
21
- }
22
- }
23
-
24
- #[napi]
25
- pub fn scrypt_hash(password_to_hash: String) -> String {
26
- return <CASScrypt as CASPasswordHasher>::hash_password(password_to_hash);
27
- }
28
-
29
- #[napi]
30
- pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> bool {
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);
56
- }
1
+ use napi_derive::napi;
2
+
3
+ use scrypt::{
4
+ password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
5
+ Scrypt,
6
+ };
7
+
8
+ use super::cas_password_hasher::CASPasswordHasher;
9
+
10
+ pub struct CASScrypt;
11
+
12
+ impl CASPasswordHasher for CASScrypt {
13
+ fn hash_password(password_to_hash: String) -> String {
14
+ let salt = SaltString::generate(&mut OsRng);
15
+ return Scrypt
16
+ .hash_password(password_to_hash.as_bytes(), &salt)
17
+ .unwrap()
18
+ .to_string();
19
+ }
20
+
21
+ fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
22
+ let parsed_hash = PasswordHash::new(&hashed_password).unwrap();
23
+ return Scrypt
24
+ .verify_password(password_to_verify.as_bytes(), &parsed_hash)
25
+ .is_ok();
26
+ }
27
+ }
28
+
29
+ #[napi]
30
+ pub fn scrypt_hash(password_to_hash: String) -> String {
31
+ return <CASScrypt as CASPasswordHasher>::hash_password(password_to_hash);
32
+ }
33
+
34
+ #[napi]
35
+ pub fn scrypt_verify(hashed_password: String, password_to_verify: String) -> bool {
36
+ return <CASScrypt as CASPasswordHasher>::verify_password(hashed_password, password_to_verify);
37
+ }
38
+
39
+ #[test]
40
+ pub fn scrypt_hash_test() {
41
+ let password = "BadPassword".to_string();
42
+ let hashed_password = scrypt_hash(password.clone());
43
+ assert_ne!(password, hashed_password);
44
+ }
45
+
46
+ #[test]
47
+ pub fn scrypt_verify_test() {
48
+ let password = "BadPassword".to_string();
49
+ let hashed_password = scrypt_hash(password.clone());
50
+ let verified = scrypt_verify(hashed_password, password);
51
+ assert_eq!(true, verified);
52
+ }
53
+
54
+ #[test]
55
+ pub fn scrypt_verify_fail_test() {
56
+ let password = "BadPassword".to_string();
57
+ let hashed_password = scrypt_hash(password.clone());
58
+ let verified = "Nope".to_string();
59
+ let verified = scrypt_verify(hashed_password, verified);
60
+ assert_eq!(false, verified);
61
+ }
@@ -1,151 +1,155 @@
1
- use aes_gcm::Key;
2
- use napi_derive::napi;
3
- use rand::rngs::OsRng;
4
- use rand::{RngCore, SeedableRng};
5
- use rand_chacha::ChaCha20Rng;
6
-
7
- use aes_gcm::{
8
- aead::{generic_array::GenericArray, Aead},
9
- Aes256Gcm, Aes128Gcm, KeyInit, Nonce
10
- };
11
-
12
- use super::cas_symmetric_encryption::{AesKeyFromX25519SharedSecret, CASAESEncryption};
13
- pub struct CASAES128;
14
- pub struct CASAES256;
15
-
16
- impl CASAESEncryption for CASAES256 {
17
- fn generate_key() -> Vec<u8> {
18
- return Aes256Gcm::generate_key(&mut OsRng).to_vec();
19
- }
20
-
21
- fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
22
- let key = GenericArray::from_slice(&aes_key);
23
- let mut cipher = Aes256Gcm::new(&key);
24
- let nonce = Nonce::from_slice(&nonce);
25
- let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap();
26
- ciphertext
27
- }
28
-
29
- fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
30
- let key = GenericArray::from_slice(&aes_key);
31
- let mut cipher = Aes256Gcm::new(&key);
32
- let nonce = Nonce::from_slice(&nonce);
33
- let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap();
34
- plaintext
35
- }
36
-
37
- fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
38
- let aes_key = Key::<Aes256Gcm>::from_slice(&shared_secret);
39
- let mut aes_nonce: [u8; 12] = Default::default();
40
- aes_nonce.copy_from_slice(&shared_secret[..12]);
41
- let result = AesKeyFromX25519SharedSecret {
42
- aes_key: aes_key.to_vec(),
43
- aes_nonce: aes_nonce.to_vec()
44
- };
45
- result
46
- }
47
- }
48
-
49
- impl CASAESEncryption for CASAES128 {
50
- fn generate_key() -> Vec<u8> {
51
- return Aes128Gcm::generate_key(&mut OsRng).to_vec();
52
- }
53
-
54
- fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
55
- let key = GenericArray::from_slice(&aes_key);
56
- let mut cipher = Aes128Gcm::new(&key);
57
- let nonce = Nonce::from_slice(&nonce);
58
- let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap();
59
- ciphertext
60
- }
61
-
62
- fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
63
- let key = GenericArray::from_slice(&aes_key);
64
- let mut cipher = Aes128Gcm::new(&key);
65
- let nonce = Nonce::from_slice(&nonce);
66
- let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap();
67
- plaintext
68
- }
69
-
70
- fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
71
- let mut aes_key: [u8; 16] = Default::default();
72
- aes_key.copy_from_slice(&shared_secret[..16]);
73
- let aes_key_slice = Key::<Aes128Gcm>::from_slice(&aes_key);
74
- let mut aes_nonce: [u8; 12] = Default::default();
75
- aes_nonce.copy_from_slice(&shared_secret[..12]);
76
- let result = AesKeyFromX25519SharedSecret {
77
- aes_key: aes_key_slice.to_vec(),
78
- aes_nonce: aes_nonce.to_vec()
79
- };
80
- result
81
- }
82
- }
83
-
84
- #[napi]
85
- pub fn aes_nonce() -> Vec<u8> {
86
- let mut rng = ChaCha20Rng::from_entropy();
87
- let mut random_bytes = Vec::with_capacity(12);
88
- random_bytes.resize(12, 0);
89
- rng.fill_bytes(&mut random_bytes);
90
- random_bytes
91
- }
92
-
93
- #[napi]
94
- pub fn aes128_key() -> Vec<u8> {
95
- return CASAES128::generate_key();
96
- }
97
-
98
- #[napi]
99
- pub fn aes256_key() -> Vec<u8> {
100
- return CASAES256::generate_key();
101
- }
102
-
103
- #[napi]
104
- pub fn aes128_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
105
- return CASAES128::encrypt_plaintext(aes_key, nonce, plaintext);
106
- }
107
-
108
- #[napi]
109
- pub fn aes128_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
110
- return CASAES128::decrypt_ciphertext(aes_key, nonce, ciphertext);
111
- }
112
-
113
- #[napi]
114
- pub fn aes256_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
115
- return CASAES256::encrypt_plaintext(aes_key, nonce, plaintext);
116
- }
117
-
118
- #[napi]
119
- pub fn aes256_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
120
- return CASAES256::decrypt_ciphertext(aes_key, nonce, ciphertext);
121
- }
122
-
123
- #[napi]
124
- pub fn aes_256_key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
125
- return CASAES256::key_from_x25519_shared_secret(shared_secret);
126
- }
127
-
128
- #[napi]
129
- pub fn aes_128_key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
130
- return CASAES128::key_from_x25519_shared_secret(shared_secret);
131
- }
132
-
133
- #[test]
134
- fn aes128_encrypt_decrypt_test() {
135
- let aes_key = aes128_key();
136
- let nonce = aes_nonce();
137
- let plaintext = b"WelcomeHome".to_vec();
138
- let ciphertext = aes128_encrypt(aes_key.clone(), nonce.clone(), plaintext.clone());
139
- let decrypted_plaintext = aes128_decrypt(aes_key, nonce, ciphertext);
140
- assert_eq!(decrypted_plaintext, plaintext)
141
- }
142
-
143
- #[test]
144
- fn aes256_encrypt_decrypt_test() {
145
- let aes_key = aes256_key();
146
- let nonce = aes_nonce();
147
- let plaintext = b"WelcomeHome".to_vec();
148
- let ciphertext = aes256_encrypt(aes_key.clone(), nonce.clone(), plaintext.clone());
149
- let decrypted_plaintext = aes256_decrypt(aes_key, nonce, ciphertext);
150
- assert_eq!(decrypted_plaintext, plaintext)
151
- }
1
+ use aes_gcm::Key;
2
+ use napi_derive::napi;
3
+ use rand::rngs::OsRng;
4
+ use rand::{RngCore, SeedableRng};
5
+ use rand_chacha::ChaCha20Rng;
6
+
7
+ use aes_gcm::{
8
+ aead::{generic_array::GenericArray, Aead},
9
+ Aes128Gcm, Aes256Gcm, KeyInit, Nonce,
10
+ };
11
+
12
+ use super::cas_symmetric_encryption::{AesKeyFromX25519SharedSecret, CASAESEncryption};
13
+ pub struct CASAES128;
14
+ pub struct CASAES256;
15
+
16
+ impl CASAESEncryption for CASAES256 {
17
+ fn generate_key() -> Vec<u8> {
18
+ return Aes256Gcm::generate_key(&mut OsRng).to_vec();
19
+ }
20
+
21
+ fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
22
+ let key = GenericArray::from_slice(&aes_key);
23
+ let mut cipher = Aes256Gcm::new(&key);
24
+ let nonce = Nonce::from_slice(&nonce);
25
+ let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap();
26
+ ciphertext
27
+ }
28
+
29
+ fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
30
+ let key = GenericArray::from_slice(&aes_key);
31
+ let mut cipher = Aes256Gcm::new(&key);
32
+ let nonce = Nonce::from_slice(&nonce);
33
+ let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap();
34
+ plaintext
35
+ }
36
+
37
+ fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
38
+ let aes_key = Key::<Aes256Gcm>::from_slice(&shared_secret);
39
+ let mut aes_nonce: [u8; 12] = Default::default();
40
+ aes_nonce.copy_from_slice(&shared_secret[..12]);
41
+ let result = AesKeyFromX25519SharedSecret {
42
+ aes_key: aes_key.to_vec(),
43
+ aes_nonce: aes_nonce.to_vec(),
44
+ };
45
+ result
46
+ }
47
+ }
48
+
49
+ impl CASAESEncryption for CASAES128 {
50
+ fn generate_key() -> Vec<u8> {
51
+ return Aes128Gcm::generate_key(&mut OsRng).to_vec();
52
+ }
53
+
54
+ fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
55
+ let key = GenericArray::from_slice(&aes_key);
56
+ let mut cipher = Aes128Gcm::new(&key);
57
+ let nonce = Nonce::from_slice(&nonce);
58
+ let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()).unwrap();
59
+ ciphertext
60
+ }
61
+
62
+ fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
63
+ let key = GenericArray::from_slice(&aes_key);
64
+ let mut cipher = Aes128Gcm::new(&key);
65
+ let nonce = Nonce::from_slice(&nonce);
66
+ let plaintext = cipher.decrypt(nonce, ciphertext.as_ref()).unwrap();
67
+ plaintext
68
+ }
69
+
70
+ fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret {
71
+ let mut aes_key: [u8; 16] = Default::default();
72
+ aes_key.copy_from_slice(&shared_secret[..16]);
73
+ let aes_key_slice = Key::<Aes128Gcm>::from_slice(&aes_key);
74
+ let mut aes_nonce: [u8; 12] = Default::default();
75
+ aes_nonce.copy_from_slice(&shared_secret[..12]);
76
+ let result = AesKeyFromX25519SharedSecret {
77
+ aes_key: aes_key_slice.to_vec(),
78
+ aes_nonce: aes_nonce.to_vec(),
79
+ };
80
+ result
81
+ }
82
+ }
83
+
84
+ #[napi]
85
+ pub fn aes_nonce() -> Vec<u8> {
86
+ let mut rng = ChaCha20Rng::from_entropy();
87
+ let mut random_bytes = Vec::with_capacity(12);
88
+ random_bytes.resize(12, 0);
89
+ rng.fill_bytes(&mut random_bytes);
90
+ random_bytes
91
+ }
92
+
93
+ #[napi]
94
+ pub fn aes128_key() -> Vec<u8> {
95
+ return CASAES128::generate_key();
96
+ }
97
+
98
+ #[napi]
99
+ pub fn aes256_key() -> Vec<u8> {
100
+ return CASAES256::generate_key();
101
+ }
102
+
103
+ #[napi]
104
+ pub fn aes128_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
105
+ return CASAES128::encrypt_plaintext(aes_key, nonce, plaintext);
106
+ }
107
+
108
+ #[napi]
109
+ pub fn aes128_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
110
+ return CASAES128::decrypt_ciphertext(aes_key, nonce, ciphertext);
111
+ }
112
+
113
+ #[napi]
114
+ pub fn aes256_encrypt(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8> {
115
+ return CASAES256::encrypt_plaintext(aes_key, nonce, plaintext);
116
+ }
117
+
118
+ #[napi]
119
+ pub fn aes256_decrypt(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8> {
120
+ return CASAES256::decrypt_ciphertext(aes_key, nonce, ciphertext);
121
+ }
122
+
123
+ #[napi]
124
+ pub fn aes_256_key_from_x25519_shared_secret(
125
+ shared_secret: Vec<u8>,
126
+ ) -> AesKeyFromX25519SharedSecret {
127
+ return CASAES256::key_from_x25519_shared_secret(shared_secret);
128
+ }
129
+
130
+ #[napi]
131
+ pub fn aes_128_key_from_x25519_shared_secret(
132
+ shared_secret: Vec<u8>,
133
+ ) -> AesKeyFromX25519SharedSecret {
134
+ return CASAES128::key_from_x25519_shared_secret(shared_secret);
135
+ }
136
+
137
+ #[test]
138
+ fn aes128_encrypt_decrypt_test() {
139
+ let aes_key = aes128_key();
140
+ let nonce = aes_nonce();
141
+ let plaintext = b"WelcomeHome".to_vec();
142
+ let ciphertext = aes128_encrypt(aes_key.clone(), nonce.clone(), plaintext.clone());
143
+ let decrypted_plaintext = aes128_decrypt(aes_key, nonce, ciphertext);
144
+ assert_eq!(decrypted_plaintext, plaintext)
145
+ }
146
+
147
+ #[test]
148
+ fn aes256_encrypt_decrypt_test() {
149
+ let aes_key = aes256_key();
150
+ let nonce = aes_nonce();
151
+ let plaintext = b"WelcomeHome".to_vec();
152
+ let ciphertext = aes256_encrypt(aes_key.clone(), nonce.clone(), plaintext.clone());
153
+ let decrypted_plaintext = aes256_decrypt(aes_key, nonce, ciphertext);
154
+ assert_eq!(decrypted_plaintext, plaintext)
155
+ }
@@ -1,14 +1,14 @@
1
- use napi_derive::napi;
2
-
3
- #[napi(constructor)]
4
- pub struct AesKeyFromX25519SharedSecret {
5
- pub aes_key: Vec<u8>,
6
- pub aes_nonce: Vec<u8>
7
- }
8
-
9
- pub trait CASAESEncryption {
10
- fn generate_key() -> Vec<u8>;
11
- fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8>;
12
- fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8>;
13
- fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret;
14
- }
1
+ use napi_derive::napi;
2
+
3
+ #[napi(constructor)]
4
+ pub struct AesKeyFromX25519SharedSecret {
5
+ pub aes_key: Vec<u8>,
6
+ pub aes_nonce: Vec<u8>,
7
+ }
8
+
9
+ pub trait CASAESEncryption {
10
+ fn generate_key() -> Vec<u8>;
11
+ fn encrypt_plaintext(aes_key: Vec<u8>, nonce: Vec<u8>, plaintext: Vec<u8>) -> Vec<u8>;
12
+ fn decrypt_ciphertext(aes_key: Vec<u8>, nonce: Vec<u8>, ciphertext: Vec<u8>) -> Vec<u8>;
13
+ fn key_from_x25519_shared_secret(shared_secret: Vec<u8>) -> AesKeyFromX25519SharedSecret;
14
+ }
@@ -1,53 +1,53 @@
1
- import { decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys, RsaKeyPairResult, signRsa, verifyRsa } from "../../index";
2
-
3
- export class RSAWrapper {
4
- public generateKeys(keySize: number): RsaKeyPairResult {
5
- if (keySize !== 1024 && keySize !== 2048 && keySize !== 4096) {
6
- throw new Error("You must provide an appropriate key size to generate RSA keys");
7
- }
8
- return generateRsaKeys(keySize);
9
- }
10
-
11
- public encrypt(publicKey: string, plaintext: Array<number>): Array<number> {
12
- if (!publicKey) {
13
- throw new Error("You must provide a public key to encrypt with RSA");
14
- }
15
- if (!plaintext || plaintext.length === 0) {
16
- throw new Error("You must provide an array of plaintext bytes to encrypt with RSA");
17
- }
18
- return encryptPlaintextRsa(publicKey, plaintext);
19
- }
20
-
21
- public decrypt(privateKey: string, ciphertext: Array<number>): Array<number> {
22
- if (!privateKey) {
23
- throw new Error("You must provide a private key to encrypt with RSA");
24
- }
25
- if (!ciphertext || ciphertext.length === 0) {
26
- throw new Error("You must provide an array of ciphertext bytes to encrypt with RSA");
27
- }
28
- return decryptCiphertextRsa(privateKey, ciphertext);
29
- }
30
-
31
- public sign(privateKey: string, hash: Array<number>): Array<number> {
32
- if (!privateKey) {
33
- throw new Error("You must provide a private key to sign with RSA");
34
- }
35
- if (!hash || hash.length === 0) {
36
- throw new Error("You must provide an allocated hash to sign with RSA");
37
- }
38
- return signRsa(privateKey, hash);
39
- }
40
-
41
- public verify(publicKey: string, hash: Array<number>, signature: Array<number>): boolean {
42
- if (!publicKey) {
43
- throw new Error("You must provide a public key to verify with RSA");
44
- }
45
- if (!hash || hash.length === 0) {
46
- throw new Error("You must provide an allocated hash to verify with RSA");
47
- }
48
- if (!signature || signature.length === 0) {
49
- throw new Error("You must provide and allocated signature to verify with RSA");
50
- }
51
- return verifyRsa(publicKey, hash, signature);
52
- }
53
- }
1
+ import { decryptCiphertextRsa, encryptPlaintextRsa, generateRsaKeys, RsaKeyPairResult, signRsa, verifyRsa } from "../../index";
2
+
3
+ export class RSAWrapper {
4
+ public generateKeys(keySize: number): RsaKeyPairResult {
5
+ if (keySize !== 1024 && keySize !== 2048 && keySize !== 4096) {
6
+ throw new Error("You must provide an appropriate key size to generate RSA keys");
7
+ }
8
+ return generateRsaKeys(keySize);
9
+ }
10
+
11
+ public encrypt(publicKey: string, plaintext: Array<number>): Array<number> {
12
+ if (!publicKey) {
13
+ throw new Error("You must provide a public key to encrypt with RSA");
14
+ }
15
+ if (!plaintext || plaintext.length === 0) {
16
+ throw new Error("You must provide an array of plaintext bytes to encrypt with RSA");
17
+ }
18
+ return encryptPlaintextRsa(publicKey, plaintext);
19
+ }
20
+
21
+ public decrypt(privateKey: string, ciphertext: Array<number>): Array<number> {
22
+ if (!privateKey) {
23
+ throw new Error("You must provide a private key to encrypt with RSA");
24
+ }
25
+ if (!ciphertext || ciphertext.length === 0) {
26
+ throw new Error("You must provide an array of ciphertext bytes to encrypt with RSA");
27
+ }
28
+ return decryptCiphertextRsa(privateKey, ciphertext);
29
+ }
30
+
31
+ public sign(privateKey: string, hash: Array<number>): Array<number> {
32
+ if (!privateKey) {
33
+ throw new Error("You must provide a private key to sign with RSA");
34
+ }
35
+ if (!hash || hash.length === 0) {
36
+ throw new Error("You must provide an allocated hash to sign with RSA");
37
+ }
38
+ return signRsa(privateKey, hash);
39
+ }
40
+
41
+ public verify(publicKey: string, hash: Array<number>, signature: Array<number>): boolean {
42
+ if (!publicKey) {
43
+ throw new Error("You must provide a public key to verify with RSA");
44
+ }
45
+ if (!hash || hash.length === 0) {
46
+ throw new Error("You must provide an allocated hash to verify with RSA");
47
+ }
48
+ if (!signature || signature.length === 0) {
49
+ throw new Error("You must provide and allocated signature to verify with RSA");
50
+ }
51
+ return verifyRsa(publicKey, hash, signature);
52
+ }
53
+ }
@@ -1,4 +1,4 @@
1
- import { RSAWrapper } from "./RSAWrapper";
2
- import { RsaKeyPairResult } from "../../index";
3
-
1
+ import { RSAWrapper } from "./RSAWrapper";
2
+ import { RsaKeyPairResult } from "../../index";
3
+
4
4
  export { RSAWrapper, RsaKeyPairResult };
@@ -0,0 +1,28 @@
1
+ import { CasrsaDigitalSignatureResult, sha512RsaDigitalSignature, sha512RsaVerifyDigitalSignature } from "../../index";
2
+ import { IDigitalSignature } from "./digital-signature-base";
3
+
4
+ export class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
5
+
6
+ createRsa(rsa_key_size: number, data_to_sign: number[]): CasrsaDigitalSignatureResult {
7
+ if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) {
8
+ throw new Error("You need to provide an appropriate RSA key size.");
9
+ }
10
+ if (data_to_sign?.length === 0) {
11
+ throw new Error("Must provide allocated data to sign");
12
+ }
13
+ return sha512RsaDigitalSignature(rsa_key_size, data_to_sign);
14
+ }
15
+
16
+ verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean {
17
+ if (!public_key) {
18
+ throw new Error("Must provide a public key");
19
+ }
20
+ if (data_to_verify?.length === 0) {
21
+ throw new Error("Must provide an allocated data to verify");
22
+ }
23
+ if (signature?.length === 0) {
24
+ throw new Error("Must provide an allocated signature");
25
+ }
26
+ return sha512RsaVerifyDigitalSignature(public_key, data_to_verify, signature);
27
+ }
28
+ }
@@ -0,0 +1,6 @@
1
+ import { CASRSADigitalSignatureResult } from "../../index";
2
+
3
+ export interface IDigitalSignature {
4
+ createRsa(rsa_key_size: number, data_to_sign: Array<number>): CASRSADigitalSignatureResult;
5
+ verifyRSa(public_key: string, data_to_verify: Array<number>, signature: Array<number>): boolean;
6
+ }
@@ -0,0 +1,19 @@
1
+ import { DigitalSignatureSHA512Wrapper } from "./digital-siganture-sha-512";
2
+ import { DigitalSignatureSHA256Wrapper } from "./digital-signaturte-sha-256";
3
+
4
+ export enum DigitalSignatureType {
5
+ SHA512 = 1,
6
+ SHA256 = 2
7
+ }
8
+
9
+ export class DigitalSignatureFactory {
10
+ public static get(type: DigitalSignatureType) {
11
+ let ds = new DigitalSignatureSHA512Wrapper();
12
+ switch (type) {
13
+ case DigitalSignatureType.SHA256:
14
+ ds = new DigitalSignatureSHA256Wrapper();
15
+ break;
16
+ }
17
+ return ds;
18
+ }
19
+ }
@@ -0,0 +1,28 @@
1
+ import { CasrsaDigitalSignatureResult, sha256RsaDigitalSignature, sha256RsaVerifyDigitalSignature } from "../../index";
2
+ import { IDigitalSignature } from "./digital-signature-base";
3
+
4
+ export class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
5
+
6
+ createRsa(rsa_key_size: number, data_to_sign: number[]): CasrsaDigitalSignatureResult {
7
+ if (rsa_key_size !== 1024 && rsa_key_size !== 2048 && rsa_key_size !== 4096) {
8
+ throw new Error("You need to provide an appropriate RSA key size.");
9
+ }
10
+ if (data_to_sign?.length === 0) {
11
+ throw new Error("Must provide allocated data to sign");
12
+ }
13
+ return sha256RsaDigitalSignature(rsa_key_size, data_to_sign);
14
+ }
15
+
16
+ verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean {
17
+ if (!public_key) {
18
+ throw new Error("Must provide a public key");
19
+ }
20
+ if (data_to_verify?.length === 0) {
21
+ throw new Error("Must provide an allocated data to verify");
22
+ }
23
+ if (signature?.length === 0) {
24
+ throw new Error("Must provide an allocated signature");
25
+ }
26
+ return sha256RsaVerifyDigitalSignature(public_key, data_to_verify, signature);
27
+ }
28
+ }