cas-typescript-sdk 1.0.15 → 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 (79) 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 +2 -0
  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-base.js +2 -0
  14. package/lib/digital-signature/digital-signature-factory.d.ts +8 -0
  15. package/lib/digital-signature/digital-signature-factory.js +22 -0
  16. package/lib/digital-signature/digital-signaturte-sha-256.d.ts +6 -0
  17. package/lib/digital-signature/digital-signaturte-sha-256.js +28 -0
  18. package/lib/digital-signature/index.d.ts +4 -0
  19. package/lib/digital-signature/index.js +8 -0
  20. package/package.json +41 -41
  21. package/src/asymmetric/cas_asymmetric_encryption.rs +15 -15
  22. package/src/asymmetric/cas_rsa.rs +88 -80
  23. package/src/digital_signature/cas_digital_signature_rsa.rs +27 -0
  24. package/src/digital_signature/sha_256_rsa.rs +96 -0
  25. package/src/digital_signature/sha_512_ed25519.rs +75 -0
  26. package/src/digital_signature/sha_512_rsa.rs +93 -0
  27. package/src/hashers/blake2.rs +37 -39
  28. package/src/hashers/cas_hasher.rs +8 -8
  29. package/src/hashers/sha.rs +102 -103
  30. package/src/key_exchange/cas_key_exchange.rs +6 -6
  31. package/src/key_exchange/x25519.rs +57 -57
  32. package/src/lib.rs +34 -27
  33. package/src/password_hashers/argon2.rs +65 -64
  34. package/src/password_hashers/bcrypt.rs +50 -51
  35. package/src/password_hashers/cas_password_hasher.rs +4 -4
  36. package/src/password_hashers/scrypt.rs +61 -56
  37. package/src/symmetric/aes.rs +155 -151
  38. package/src/symmetric/cas_symmetric_encryption.rs +14 -14
  39. package/src-ts/asymmetric/RSAWrapper.ts +53 -53
  40. package/src-ts/asymmetric/index.ts +3 -3
  41. package/src-ts/digital-signature/digital-siganture-sha-512.ts +28 -0
  42. package/src-ts/digital-signature/digital-signature-base.ts +6 -0
  43. package/src-ts/digital-signature/digital-signature-factory.ts +19 -0
  44. package/src-ts/digital-signature/digital-signaturte-sha-256.ts +28 -0
  45. package/src-ts/digital-signature/index.ts +4 -0
  46. package/src-ts/global.d.ts +1 -1
  47. package/src-ts/hashers/hasher-base.ts +5 -5
  48. package/src-ts/hashers/hasher-factory.ts +11 -11
  49. package/src-ts/hashers/hasher-type.ts +2 -2
  50. package/src-ts/hashers/index.ts +5 -5
  51. package/src-ts/hashers/sha-wrapper.ts +37 -37
  52. package/src-ts/helpers/nonce-generator.ts +8 -8
  53. package/src-ts/hybrid/hybrid-encryption-wrapper.ts +64 -64
  54. package/src-ts/hybrid/index.ts +9 -9
  55. package/src-ts/hybrid/types/aes-rsa-hybird-encrypt-result.ts +12 -12
  56. package/src-ts/hybrid/types/aes-rsa-hybrid-initializer.ts +23 -23
  57. package/src-ts/index.ts +34 -34
  58. package/src-ts/key_exchange/index.ts +3 -3
  59. package/src-ts/key_exchange/x25519.ts +10 -10
  60. package/src-ts/password-hashers/argon2-wrapper.ts +18 -18
  61. package/src-ts/password-hashers/bcrypt-wrapper.ts +23 -23
  62. package/src-ts/password-hashers/index.ts +14 -14
  63. package/src-ts/password-hashers/password-hasher-base.ts +3 -3
  64. package/src-ts/password-hashers/password-hasher-factory.ts +20 -20
  65. package/src-ts/password-hashers/password-hasher-type.ts +4 -4
  66. package/src-ts/password-hashers/scrypt-wrapper.ts +19 -19
  67. package/src-ts/symmetric/aes-wrapper.ts +50 -50
  68. package/src-ts/symmetric/index.ts +3 -3
  69. package/test-ts/asymmetric.test.spec.ts +27 -27
  70. package/test-ts/digital-signature.test.spec.ts +49 -0
  71. package/test-ts/hasher.test.spec.ts +70 -70
  72. package/test-ts/helpers/array.ts +9 -9
  73. package/test-ts/hybrid.test.spec.ts +33 -33
  74. package/test-ts/insecure-channel.test.spec.ts +50 -50
  75. package/test-ts/key-exchange-test.spec.ts +23 -23
  76. package/test-ts/password-hasher-test.spec.ts +102 -102
  77. package/test-ts/symmetric.test.spec.ts +31 -31
  78. package/tsconfig.json +21 -21
  79. package/build-node.sh +0 -2
@@ -0,0 +1,28 @@
1
+ # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
+
4
+ name: Linux Build / Test
5
+
6
+ on:
7
+ pull_request:
8
+ branches: [ "main" ]
9
+
10
+ jobs:
11
+ build:
12
+
13
+ runs-on: ubuntu-latest
14
+
15
+ strategy:
16
+ matrix:
17
+ node-version: [20.x]
18
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
19
+
20
+ steps:
21
+ - uses: actions/checkout@v3
22
+ - name: Use Node.js ${{ matrix.node-version }}
23
+ uses: actions/setup-node@v3
24
+ with:
25
+ node-version: ${{ matrix.node-version }}
26
+ cache: 'npm'
27
+ - run: npm ci
28
+ - run: npm test
@@ -0,0 +1,28 @@
1
+ # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
+
4
+ name: Windows Build / Test
5
+
6
+ on:
7
+ pull_request:
8
+ branches: [ "main" ]
9
+
10
+ jobs:
11
+ build:
12
+
13
+ runs-on: windows-latest
14
+
15
+ strategy:
16
+ matrix:
17
+ node-version: [20.x]
18
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
19
+
20
+ steps:
21
+ - uses: actions/checkout@v3
22
+ - name: Use Node.js ${{ matrix.node-version }}
23
+ uses: actions/setup-node@v3
24
+ with:
25
+ node-version: ${{ matrix.node-version }}
26
+ cache: 'npm'
27
+ - run: npm ci
28
+ - run: npm test
@@ -0,0 +1,32 @@
1
+ # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
+
4
+ name: Node.js CI
5
+
6
+ on:
7
+ push:
8
+ branches: [ "main" ]
9
+
10
+ jobs:
11
+ build:
12
+
13
+ runs-on: ubuntu-latest
14
+
15
+ strategy:
16
+ matrix:
17
+ node-version: [20.x]
18
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
19
+
20
+ steps:
21
+ - uses: actions/checkout@v3
22
+ - name: Use Node.js ${{ matrix.node-version }}
23
+ uses: actions/setup-node@v3
24
+ with:
25
+ node-version: ${{ matrix.node-version }}
26
+ registry-url: 'https://registry.npmjs.org'
27
+ cache: 'npm'
28
+ - run: npm ci
29
+ - run: npm test
30
+ - run: npm publish
31
+ env:
32
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/Cargo.toml CHANGED
@@ -19,7 +19,14 @@ rand_chacha = "0.3.1"
19
19
  rsa = "0.9.6"
20
20
  scrypt = "0.11.0"
21
21
  sha3 = "0.10.8"
22
- x25519-dalek = {version = "2.0.0", features = ["static_secrets"] }
22
+ x25519-dalek = {version = "2.0.0", features = ["static_secrets"]}
23
+ rand_07 = { package = "rand", version = "0.7.0" }
24
+
25
+ [profile.dev.package.num-bigint-dig]
26
+ opt-level = 3
27
+
28
+ [dependencies.ed25519-dalek]
29
+ version = "1"
23
30
 
24
31
  [build-dependencies]
25
32
  napi-build = "1"
package/README.md CHANGED
@@ -4,6 +4,8 @@ Ever wanted all of your most useful cryptograpihc operations in one module and n
4
4
  CAS is here to provide a unified development experience as an abstract layer to the RustCrypto and Dalek-Cryptography suite of algorithms.
5
5
  The official NPM page can be found [here](https://www.npmjs.com/package/cas-typescript-sdk).
6
6
 
7
+ ## [Examples](./docs/EXAMPLES.md)
8
+
7
9
  ## Consuming Library Documentation
8
10
  **Note: All work is experimental and we understand some benchmarks might not be the most optimal.**
9
11
 
package/build.rs CHANGED
@@ -1,5 +1,5 @@
1
- extern crate napi_build;
2
-
3
- fn main() {
4
- napi_build::setup();
5
- }
1
+ extern crate napi_build;
2
+
3
+ fn main() {
4
+ napi_build::setup();
5
+ }
@@ -0,0 +1,39 @@
1
+ ### Symmetric
2
+ - AES 256
3
+ ```typescript
4
+ const aesWrapper: AESWrapper = new AESWrapper();
5
+ const aesKey = aesWrapper.aes128Key();
6
+ const aesNonce = aesWrapper.aesNonce();
7
+ const toEncrypt: string = "This is my array to encrypt";
8
+ const encoder = new TextEncoder();
9
+ const tohashBytes: Array<number> = Array.from(encoder.encode(toEncrypt));
10
+ const ciphertext = aesWrapper.aes128Encrypt(aesKey, aesNonce, tohashBytes);
11
+ const plaintxt = aesWrapper.aes128Decrypt(aesKey, aesNonce, ciphertext);
12
+ ```
13
+
14
+
15
+ ### Passwords
16
+ - BCrypt
17
+ ```typescript
18
+ const hasher: BCryptWrapper = new BCryptWrapper();
19
+ const password: string = "ThisOneBadPassword!@";
20
+ const hashedPassword: string = hasher.hashPassword(password);
21
+ ```
22
+
23
+ - SCrypt
24
+ ```typescript
25
+ const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
26
+ PasswordHasherType.Scrypt,
27
+ );
28
+ const password: string = "ScryptRocks";
29
+ const hashed: string = hasher.hashPassword(password);
30
+ ```
31
+
32
+ - Argon2
33
+ ```typescript
34
+ const hasher: ScryptWrapper = PasswordHasherFactory.getHasher(
35
+ PasswordHasherType.Argon2,
36
+ );
37
+ const password: string = "ScryptRocks";
38
+ const hashed: string = hasher.hashPassword(password);
39
+ ```
package/index.d.ts CHANGED
@@ -29,6 +29,12 @@ export function encryptPlaintextRsa(publicKey: string, plaintext: Array<number>)
29
29
  export function decryptCiphertextRsa(privateKey: string, ciphertext: Array<number>): Array<number>
30
30
  export function signRsa(privateKey: string, hash: Array<number>): Array<number>
31
31
  export function verifyRsa(publicKey: string, hash: Array<number>, signature: Array<number>): boolean
32
+ export function sha512RsaDigitalSignature(rsaKeySize: number, dataToSign: Array<number>): CasrsaDigitalSignatureResult
33
+ export function sha512RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array<number>, signature: Array<number>): boolean
34
+ export function sha256RsaDigitalSignature(rsaKeySize: number, dataToSign: Array<number>): CasrsaDigitalSignatureResult
35
+ export function sha256RsaVerifyDigitalSignature(publicKey: string, dataToVerify: Array<number>, signature: Array<number>): boolean
36
+ export function sha512Ed25519DigitalSignature(dataToSign: Array<number>): Casshaed25519DalekDigitalSignatureResult
37
+ export function sha512Ed25519DigitalSignatureVerify(publicKey: Array<number>, dataToVerify: Array<number>, signature: Array<number>): boolean
32
38
  export type x25519SecretPublicKeyResult = X25519SecretPublicKeyResult
33
39
  export class X25519SecretPublicKeyResult {
34
40
  publicKey: Array<number>
@@ -46,3 +52,16 @@ export class RsaKeyPairResult {
46
52
  publicKey: string
47
53
  constructor(privateKey: string, publicKey: string)
48
54
  }
55
+ export type CASRSADigitalSignatureResult = CasrsaDigitalSignatureResult
56
+ export class CasrsaDigitalSignatureResult {
57
+ publicKey: string
58
+ privateKey: string
59
+ signature: Array<number>
60
+ constructor(publicKey: string, privateKey: string, signature: Array<number>)
61
+ }
62
+ export type CASSHAED25519DalekDigitalSignatureResult = Casshaed25519DalekDigitalSignatureResult
63
+ export class Casshaed25519DalekDigitalSignatureResult {
64
+ publicKey: Array<number>
65
+ signature: Array<number>
66
+ constructor(publicKey: Array<number>, signature: Array<number>)
67
+ }
package/index.node CHANGED
Binary file
@@ -0,0 +1,6 @@
1
+ import { CasrsaDigitalSignatureResult } from "../../index";
2
+ import { IDigitalSignature } from "./digital-signature-base";
3
+ export declare class DigitalSignatureSHA512Wrapper implements IDigitalSignature {
4
+ createRsa(rsa_key_size: number, data_to_sign: number[]): CasrsaDigitalSignatureResult;
5
+ verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean;
6
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DigitalSignatureSHA512Wrapper = void 0;
4
+ const index_1 = require("../../index");
5
+ class DigitalSignatureSHA512Wrapper {
6
+ createRsa(rsa_key_size, data_to_sign) {
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 (0, index_1.sha512RsaDigitalSignature)(rsa_key_size, data_to_sign);
14
+ }
15
+ verifyRSa(public_key, data_to_verify, signature) {
16
+ if (!public_key) {
17
+ throw new Error("Must provide a public key");
18
+ }
19
+ if (data_to_verify?.length === 0) {
20
+ throw new Error("Must provide an allocated data to verify");
21
+ }
22
+ if (signature?.length === 0) {
23
+ throw new Error("Must provide an allocated signature");
24
+ }
25
+ return (0, index_1.sha512RsaVerifyDigitalSignature)(public_key, data_to_verify, signature);
26
+ }
27
+ }
28
+ exports.DigitalSignatureSHA512Wrapper = DigitalSignatureSHA512Wrapper;
@@ -0,0 +1,5 @@
1
+ import { CASRSADigitalSignatureResult } from "../../index";
2
+ export interface IDigitalSignature {
3
+ createRsa(rsa_key_size: number, data_to_sign: Array<number>): CASRSADigitalSignatureResult;
4
+ verifyRSa(public_key: string, data_to_verify: Array<number>, signature: Array<number>): boolean;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { DigitalSignatureSHA512Wrapper } from "./digital-siganture-sha-512";
2
+ export declare enum DigitalSignatureType {
3
+ SHA512 = 1,
4
+ SHA256 = 2
5
+ }
6
+ export declare class DigitalSignatureFactory {
7
+ static get(type: DigitalSignatureType): DigitalSignatureSHA512Wrapper;
8
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DigitalSignatureFactory = exports.DigitalSignatureType = void 0;
4
+ const digital_siganture_sha_512_1 = require("./digital-siganture-sha-512");
5
+ const digital_signaturte_sha_256_1 = require("./digital-signaturte-sha-256");
6
+ var DigitalSignatureType;
7
+ (function (DigitalSignatureType) {
8
+ DigitalSignatureType[DigitalSignatureType["SHA512"] = 1] = "SHA512";
9
+ DigitalSignatureType[DigitalSignatureType["SHA256"] = 2] = "SHA256";
10
+ })(DigitalSignatureType || (exports.DigitalSignatureType = DigitalSignatureType = {}));
11
+ class DigitalSignatureFactory {
12
+ static get(type) {
13
+ let ds = new digital_siganture_sha_512_1.DigitalSignatureSHA512Wrapper();
14
+ switch (type) {
15
+ case DigitalSignatureType.SHA256:
16
+ ds = new digital_signaturte_sha_256_1.DigitalSignatureSHA256Wrapper();
17
+ break;
18
+ }
19
+ return ds;
20
+ }
21
+ }
22
+ exports.DigitalSignatureFactory = DigitalSignatureFactory;
@@ -0,0 +1,6 @@
1
+ import { CasrsaDigitalSignatureResult } from "../../index";
2
+ import { IDigitalSignature } from "./digital-signature-base";
3
+ export declare class DigitalSignatureSHA256Wrapper implements IDigitalSignature {
4
+ createRsa(rsa_key_size: number, data_to_sign: number[]): CasrsaDigitalSignatureResult;
5
+ verifyRSa(public_key: string, data_to_verify: number[], signature: number[]): boolean;
6
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DigitalSignatureSHA256Wrapper = void 0;
4
+ const index_1 = require("../../index");
5
+ class DigitalSignatureSHA256Wrapper {
6
+ createRsa(rsa_key_size, data_to_sign) {
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 (0, index_1.sha256RsaDigitalSignature)(rsa_key_size, data_to_sign);
14
+ }
15
+ verifyRSa(public_key, data_to_verify, signature) {
16
+ if (!public_key) {
17
+ throw new Error("Must provide a public key");
18
+ }
19
+ if (data_to_verify?.length === 0) {
20
+ throw new Error("Must provide an allocated data to verify");
21
+ }
22
+ if (signature?.length === 0) {
23
+ throw new Error("Must provide an allocated signature");
24
+ }
25
+ return (0, index_1.sha256RsaVerifyDigitalSignature)(public_key, data_to_verify, signature);
26
+ }
27
+ }
28
+ exports.DigitalSignatureSHA256Wrapper = DigitalSignatureSHA256Wrapper;
@@ -0,0 +1,4 @@
1
+ export declare enum DigitalSignatureType {
2
+ SHA512 = 1,
3
+ SHA256 = 2
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DigitalSignatureType = void 0;
4
+ var DigitalSignatureType;
5
+ (function (DigitalSignatureType) {
6
+ DigitalSignatureType[DigitalSignatureType["SHA512"] = 1] = "SHA512";
7
+ DigitalSignatureType[DigitalSignatureType["SHA256"] = 2] = "SHA256";
8
+ })(DigitalSignatureType || (exports.DigitalSignatureType = DigitalSignatureType = {}));
package/package.json CHANGED
@@ -1,41 +1,41 @@
1
- {
2
- "name": "cas-typescript-sdk",
3
- "version": "1.0.15",
4
- "description": "",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "scripts": {
8
- "test": "cargo test && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
9
- "node:test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
10
- "rust:test": "cargo test",
11
- "build": "npm run build:rust && npm run build:node",
12
- "build:rust": "napi build --release",
13
- "build:node": "bash build-node.sh",
14
- "prepare": "npm run build"
15
- },
16
- "repository": {
17
- "type": "git",
18
- "url": "git+https://github.com/Cryptographic-API-Services/cas-typescript-sdk"
19
- },
20
- "keywords": [],
21
- "author": "Mike Mulchrone <mikemulchrone987@gmail.com>",
22
- "license": "Apache 2.0",
23
- "bugs": {
24
- "url": "https://github.com/Cryptographic-API-Services/cas-typescript-sdk/issues"
25
- },
26
- "homepage": "https://github.com/Cryptographic-API-Services/cas-typescript-sdk#readme",
27
- "publishConfig": {
28
- "access": "public",
29
- "registry": "https://registry.npmjs.org/"
30
- },
31
- "devDependencies": {
32
- "@napi-rs/cli": "^2.17.0",
33
- "@types/chai": "^4.3.11",
34
- "@types/mocha": "^10.0.6",
35
- "@types/node-fetch": "^2.6.3",
36
- "chai": "^4.4.1",
37
- "mocha": "^10.2.0",
38
- "ts-node": "^10.9.1",
39
- "typescript": "^5.0.3"
40
- }
41
- }
1
+
2
+ {
3
+ "name": "cas-typescript-sdk",
4
+ "version": "1.0.16",
5
+ "description": "",
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "scripts": {
9
+ "test": "cargo test && npm run build && mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
10
+ "node:test": "mocha -r ts-node/register ./test-ts/**/*.ts --timeout 20000 --recursive",
11
+ "rust:test": "cargo test",
12
+ "build": "npm run build:rust && rm -rf lib && tsc",
13
+ "build:rust": "napi build --release",
14
+ "prepare": "npm run build"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/Cryptographic-API-Services/cas-typescript-sdk"
19
+ },
20
+ "keywords": [],
21
+ "author": "Mike Mulchrone <mikemulchrone987@gmail.com>",
22
+ "license": "Apache 2.0",
23
+ "bugs": {
24
+ "url": "https://github.com/Cryptographic-API-Services/cas-typescript-sdk/issues"
25
+ },
26
+ "homepage": "https://github.com/Cryptographic-API-Services/cas-typescript-sdk#readme",
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "registry": "https://registry.npmjs.org/"
30
+ },
31
+ "devDependencies": {
32
+ "@napi-rs/cli": "^2.17.0",
33
+ "@types/chai": "^4.3.11",
34
+ "@types/mocha": "^10.0.6",
35
+ "@types/node-fetch": "^2.6.3",
36
+ "chai": "^4.4.1",
37
+ "mocha": "^10.2.0",
38
+ "ts-node": "^10.9.1",
39
+ "typescript": "^5.0.3"
40
+ }
41
+ }
@@ -1,15 +1,15 @@
1
- use napi_derive::napi;
2
-
3
- #[napi(constructor)]
4
- pub struct RSAKeyPairResult {
5
- pub private_key: String,
6
- pub public_key: String
7
- }
8
-
9
- pub trait CASRSAEncryption {
10
- fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult;
11
- fn encrypt_plaintext(public_key: String, plaintext: Vec<u8>) -> Vec<u8>;
12
- fn decrypt_ciphertext(private_key: String, ciphertext: Vec<u8>) -> Vec<u8>;
13
- fn sign(private_key: String, hash: Vec<u8>) -> Vec<u8>;
14
- fn verify(public_key: String, hash: Vec<u8>, signed_text: Vec<u8>) -> bool;
15
- }
1
+ use napi_derive::napi;
2
+
3
+ #[napi(constructor)]
4
+ pub struct RSAKeyPairResult {
5
+ pub private_key: String,
6
+ pub public_key: String,
7
+ }
8
+
9
+ pub trait CASRSAEncryption {
10
+ fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult;
11
+ fn encrypt_plaintext(public_key: String, plaintext: Vec<u8>) -> Vec<u8>;
12
+ fn decrypt_ciphertext(private_key: String, ciphertext: Vec<u8>) -> Vec<u8>;
13
+ fn sign(private_key: String, hash: Vec<u8>) -> Vec<u8>;
14
+ fn verify(public_key: String, hash: Vec<u8>, signed_text: Vec<u8>) -> bool;
15
+ }
@@ -1,80 +1,88 @@
1
- use napi_derive::napi;
2
- use rand::rngs::OsRng;
3
- use rsa::{
4
- pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey}, pkcs8::{DecodePrivateKey, EncodePrivateKey}, Pkcs1v15Encrypt, Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey
5
- };
6
-
7
- use super::cas_asymmetric_encryption::{CASRSAEncryption, RSAKeyPairResult};
8
- pub struct CASRSA;
9
-
10
- impl CASRSAEncryption for CASRSA {
11
- fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult {
12
- let mut rng: OsRng = OsRng;
13
- let private_key: RsaPrivateKey = RsaPrivateKey::new(&mut rng, key_size as usize).expect("failed to generate a key");
14
- let public_key: RsaPublicKey = private_key.to_public_key();
15
- let result = RSAKeyPairResult {
16
- public_key: public_key.to_pkcs1_pem(rsa::pkcs1::LineEnding::LF).unwrap().to_string(),
17
- private_key: private_key.to_pkcs8_pem(rsa::pkcs8::LineEnding::LF).unwrap().to_string()
18
- };
19
- result
20
- }
21
-
22
- fn encrypt_plaintext(public_key: String, plaintext: Vec<u8>) -> Vec<u8> {
23
- let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap();
24
- let mut rng = rand::thread_rng();
25
- let ciphertext = public_key.encrypt(&mut rng, Pkcs1v15Encrypt, &plaintext).unwrap();
26
- ciphertext
27
- }
28
-
29
- fn decrypt_ciphertext(private_key: String, ciphertext: Vec<u8>) -> Vec<u8> {
30
- let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key).unwrap();
31
- let plaintext = private_key.decrypt(Pkcs1v15Encrypt, &ciphertext).unwrap();
32
- plaintext
33
- }
34
-
35
- fn sign(private_key: String, hash: Vec<u8>) -> Vec<u8> {
36
- let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key).unwrap();
37
- let mut signed_data = private_key.sign(Pkcs1v15Sign::new_unprefixed(), &hash).unwrap();
38
- signed_data
39
- }
40
-
41
- fn verify(public_key: String, hash: Vec<u8>, signature: Vec<u8>) -> bool {
42
- let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap();
43
- let verified = public_key.verify(
44
- Pkcs1v15Sign::new_unprefixed(),
45
- &hash,
46
- &signature,
47
- );
48
- if verified.is_err() == false {
49
- return true;
50
- } else {
51
- return false;
52
- }
53
- }
54
- }
55
-
56
-
57
- #[napi]
58
- pub fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult {
59
- return CASRSA::generate_rsa_keys(key_size);
60
- }
61
-
62
- #[napi]
63
- pub fn encrypt_plaintext_rsa(public_key: String, plaintext: Vec<u8>) -> Vec<u8> {
64
- return CASRSA::encrypt_plaintext(public_key, plaintext);
65
- }
66
-
67
- #[napi]
68
- pub fn decrypt_ciphertext_rsa(private_key: String, ciphertext: Vec<u8>) -> Vec<u8> {
69
- return CASRSA::decrypt_ciphertext(private_key, ciphertext);
70
- }
71
-
72
- #[napi]
73
- pub fn sign_rsa(private_key: String, hash: Vec<u8>) -> Vec<u8> {
74
- return CASRSA::sign(private_key, hash);
75
- }
76
-
77
- #[napi]
78
- pub fn verify_rsa(public_key: String, hash: Vec<u8>, signature: Vec<u8>) -> bool {
79
- return CASRSA::verify(public_key, hash, signature);
80
- }
1
+ use napi_derive::napi;
2
+ use rand::rngs::OsRng;
3
+ use rsa::{
4
+ pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey},
5
+ pkcs8::{DecodePrivateKey, EncodePrivateKey},
6
+ Pkcs1v15Encrypt, Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey,
7
+ };
8
+
9
+ use super::cas_asymmetric_encryption::{CASRSAEncryption, RSAKeyPairResult};
10
+ pub struct CASRSA;
11
+
12
+ impl CASRSAEncryption for CASRSA {
13
+ fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult {
14
+ let mut rng: OsRng = OsRng;
15
+ let private_key: RsaPrivateKey =
16
+ RsaPrivateKey::new(&mut rng, key_size as usize).expect("failed to generate a key");
17
+ let public_key: RsaPublicKey = private_key.to_public_key();
18
+ let result = RSAKeyPairResult {
19
+ public_key: public_key
20
+ .to_pkcs1_pem(rsa::pkcs1::LineEnding::LF)
21
+ .unwrap()
22
+ .to_string(),
23
+ private_key: private_key
24
+ .to_pkcs8_pem(rsa::pkcs8::LineEnding::LF)
25
+ .unwrap()
26
+ .to_string(),
27
+ };
28
+ result
29
+ }
30
+
31
+ fn encrypt_plaintext(public_key: String, plaintext: Vec<u8>) -> Vec<u8> {
32
+ let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap();
33
+ let mut rng = rand::thread_rng();
34
+ let ciphertext = public_key
35
+ .encrypt(&mut rng, Pkcs1v15Encrypt, &plaintext)
36
+ .unwrap();
37
+ ciphertext
38
+ }
39
+
40
+ fn decrypt_ciphertext(private_key: String, ciphertext: Vec<u8>) -> Vec<u8> {
41
+ let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key).unwrap();
42
+ let plaintext = private_key.decrypt(Pkcs1v15Encrypt, &ciphertext).unwrap();
43
+ plaintext
44
+ }
45
+
46
+ fn sign(private_key: String, hash: Vec<u8>) -> Vec<u8> {
47
+ let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key).unwrap();
48
+ let mut signed_data = private_key
49
+ .sign(Pkcs1v15Sign::new_unprefixed(), &hash)
50
+ .unwrap();
51
+ signed_data
52
+ }
53
+
54
+ fn verify(public_key: String, hash: Vec<u8>, signature: Vec<u8>) -> bool {
55
+ let public_key = RsaPublicKey::from_pkcs1_pem(&public_key).unwrap();
56
+ let verified = public_key.verify(Pkcs1v15Sign::new_unprefixed(), &hash, &signature);
57
+ if verified.is_err() == false {
58
+ return true;
59
+ } else {
60
+ return false;
61
+ }
62
+ }
63
+ }
64
+
65
+ #[napi]
66
+ pub fn generate_rsa_keys(key_size: u32) -> RSAKeyPairResult {
67
+ return CASRSA::generate_rsa_keys(key_size);
68
+ }
69
+
70
+ #[napi]
71
+ pub fn encrypt_plaintext_rsa(public_key: String, plaintext: Vec<u8>) -> Vec<u8> {
72
+ return CASRSA::encrypt_plaintext(public_key, plaintext);
73
+ }
74
+
75
+ #[napi]
76
+ pub fn decrypt_ciphertext_rsa(private_key: String, ciphertext: Vec<u8>) -> Vec<u8> {
77
+ return CASRSA::decrypt_ciphertext(private_key, ciphertext);
78
+ }
79
+
80
+ #[napi]
81
+ pub fn sign_rsa(private_key: String, hash: Vec<u8>) -> Vec<u8> {
82
+ return CASRSA::sign(private_key, hash);
83
+ }
84
+
85
+ #[napi]
86
+ pub fn verify_rsa(public_key: String, hash: Vec<u8>, signature: Vec<u8>) -> bool {
87
+ return CASRSA::verify(public_key, hash, signature);
88
+ }
@@ -0,0 +1,27 @@
1
+ use napi_derive::napi;
2
+
3
+ #[napi(constructor)]
4
+ pub struct CASRSADigitalSignatureResult {
5
+ pub public_key: String,
6
+ pub private_key: String,
7
+ pub signature: Vec<u8>,
8
+ }
9
+
10
+ #[napi(constructor)]
11
+ pub struct CASSHAED25519DalekDigitalSignatureResult {
12
+ pub public_key: Vec<u8>,
13
+ pub signature: Vec<u8>
14
+ }
15
+
16
+ pub trait CASRSADigitalSignature {
17
+ fn digital_signature_rsa(
18
+ rsa_key_size: u32,
19
+ data_to_sign: Vec<u8>,
20
+ ) -> CASRSADigitalSignatureResult;
21
+ fn verify_rsa(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool;
22
+ }
23
+
24
+ pub trait CASED25519DigitalSignature {
25
+ fn digital_signature_ed25519(data_to_sign: Vec<u8>) -> CASSHAED25519DalekDigitalSignatureResult;
26
+ fn digital_signature_ed25519_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool;
27
+ }