Task: Completed for cryptojs and tests functions and validate keys

This commit is contained in:
Sambo Chea 2022-02-14 12:30:32 +07:00
parent 9d2a43feb9
commit fd27e6f9dc
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
23 changed files with 324 additions and 24 deletions

1
dummy/ase-256-16.key Normal file
View File

@ -0,0 +1 @@
6ef348dd9b0b4f29e47f4f1c9d7b6255

1
dummy/ase-256-32.key Normal file
View File

@ -0,0 +1 @@
56773703de5fdbe4292749c2505b6059a6698aad7881065df0818442f7d559b2

4
dummy/ase-256.json Normal file
View File

@ -0,0 +1,4 @@
{
"key": "56773703de5fdbe4292749c2505b6059a6698aad7881065df0818442f7d559b2",
"iv": "6ef348dd9b0b4f29e47f4f1c9d7b6255"
}

4
dummy/ase-256.keys.json Normal file
View File

@ -0,0 +1,4 @@
{
"keyPath": "./dummy/ase-256-32.key",
"ivPath": "./dummy/ase-256-16.key"
}

1
dummy/private-key.pem Normal file
View File

@ -0,0 +1 @@
-----BEGIN RSA PRIVATE KEY----- MIIJKQIBAAKCAgEA0PfjoO54+QZui0EfssxGvHOm8bhDzQIIDcH/jUvN25E28MYF VOy1CgDk2n25eakhaaK8ogmP9PfczGpLalCGfVACCXNQFc60k6zjQY8lNY6DY2NJ 4qGR5OZsA2lhXgYtptqHCvGnqqIBpfYkddKzZOjQGZvUQ3XVZvEpkDuJGvPFW/+B 4NjsgSK0bJzygnJnbJYUHezBVRQynJRIxJmCZfuhqJM2w0FLB1/8TZVn9w3OC3DX avU7DfEGOlJb9evRrvdVGag14Pcy6SXoZ1jFscRJuXyWy1mLooTx6osj7V6VYhnK ZJrlRTAUxnGZLuBdqYztU7SNhZfmuCSIS+nduqrdM9P3NPqgOqmIMTps0fcQg0DU 5CFdWIy/FecErg1YaAhbEOqQO4QQlTmMDFFJ63Y9rE3s9W5k30ZP8Ydxa8UT3su3 6ePzUGzWXUu2M8ffc8nnfiAvh1XgJiBqDJkmVEG3GzmDL0BfDLqMnhBJ2UaQPXK9 w52masDgvHv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uWcR/+C72fBEU4iA/h4rdH4PVMTP99YhN+Zd4w9UM2tITafCM3RdNbKZ3f+oXfF CjqNKiJI/VSLkErOKQnw/AiGT8dpmmFGl+DazUsYNEP3ScFUAI+ZqsUyh5q9SoCn YmcpuMbXLRLVYQJQASkYwBkCAwEAAQ== -----END PUBLIC KEY-----"
}

View File

@ -0,0 +1,4 @@
{
"privateKeyPath": "./dummy/private-key.pem",
"publicKeyPath": "./dummy/public-key.pem"
}

1
dummy/public-key.pem Normal file
View File

@ -0,0 +1 @@
-----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0PfjoO54+QZui0EfssxG vHOm8bhDzQIIDcH/jUvN25E28MYFVOy1CgDk2n25eakhaaK8ogmP9PfczGpLalCG fVACCXNQFc60k6zjQY8lNY6DY2NJ4qGR5OZsA2lhXgYtptqHCvGnqqIBpfYkddKz ZOjQGZvUQ3XVZvEpkDuJGvPFW/+B4NjsgSK0bJzygnJnbJYUHezBVRQynJRIxJmC ZfuhqJM2w0FLB1/8TZVn9w3OC3DXavU7DfEGOlJb9evRrvdVGag14Pcy6SXoZ1jF scRJuXyWy1mLooTx6osj7V6VYhnKZJrlRTAUxnGZLuBdqYztU7SNhZfmuCSIS+nd uqrdM9P3NPqgOqmIMTps0fcQg0DU5CFdWIy/FecErg1YaAhbEOqQO4QQlTmMDFFJ 63Y9rE3s9W5k30ZP8Ydxa8UT3su36ePzUGzWXUu2M8ffc8nnfiAvh1XgJiBqDJkm VEG3GzmDL0BfDLqMnhBJ2UaQPXK9w52masDgvHvyMEZoBaFBHtVICl4Jek6O9KSz 1uWcR/+C72fBEU4iA/h4rdH4PVMTP99YhN+Zd4w9UM2tITafCM3RdNbKZ3f+oXfF CjqNKiJI/VSLkErOKQnw/AiGT8dpmmFGl+DazUsYNEP3ScFUAI+ZqsUyh5q9SoCn YmcpuMbXLRLVYQJQASkYwBkCAwEAAQ== -----END PUBLIC KEY-----

5
jest.config.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
modulePathIgnorePatterns: ["<rootDir>/build/"],
}

View File

@ -7,7 +7,7 @@
"start": "ts-node-dev --respawn --transpile-only src/index.ts", "start": "ts-node-dev --respawn --transpile-only src/index.ts",
"build": "rm -rf dist && tsc", "build": "rm -rf dist && tsc",
"serve": "npm run build && node dist/index.js", "serve": "npm run build && node dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1", "test": "jest",
"prepare": "husky install" "prepare": "husky install"
}, },
"repository": { "repository": {
@ -23,6 +23,8 @@
"author": "Sambo Chea <sombochea@cubetiqs.com>", "author": "Sambo Chea <sombochea@cubetiqs.com>",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^16.9.1",
"@typescript-eslint/eslint-plugin": "^4.30.0", "@typescript-eslint/eslint-plugin": "^4.30.0",
"@typescript-eslint/parser": "^4.30.0", "@typescript-eslint/parser": "^4.30.0",
"eslint": "^7.32.0", "eslint": "^7.32.0",
@ -30,11 +32,12 @@
"eslint-plugin-jsdoc": "^36.0.8", "eslint-plugin-jsdoc": "^36.0.8",
"eslint-plugin-prefer-arrow": "^1.2.3", "eslint-plugin-prefer-arrow": "^1.2.3",
"husky": "^7.0.2", "husky": "^7.0.2",
"jest": "^27.5.1",
"lint-staged": "^11.1.2", "lint-staged": "^11.1.2",
"prettier": "2.3.2", "prettier": "2.3.2",
"ts-jest": "^27.1.3",
"ts-node-dev": "^1.1.8", "ts-node-dev": "^1.1.8",
"typescript": "^4.4.2", "typescript": "^4.4.2"
"@types/node": "^16.9.1"
}, },
"lint-staged": { "lint-staged": {
"**/*": "prettier --write --ignore-unknown" "**/*": "prettier --write --ignore-unknown"

View File

@ -3,11 +3,11 @@ import {
DEFAULT_AES_ALGORITHM, DEFAULT_AES_ALGORITHM,
DEFAULT_BUFFER_TYPE, DEFAULT_BUFFER_TYPE,
DEFAULT_ENCODING_TYPE, DEFAULT_ENCODING_TYPE,
} from "../config" } from "../../config"
import { stringToBuffer } from "../util" import { bufferToString, stringToBuffer } from "../../util"
const encrypt = ( const encrypt = (
data: string, data: string | Buffer,
key: string | Buffer, key: string | Buffer,
iv?: string | Buffer | null iv?: string | Buffer | null
): string => { ): string => {
@ -16,8 +16,9 @@ const encrypt = (
stringToBuffer(key), stringToBuffer(key),
stringToBuffer(iv || key) stringToBuffer(iv || key)
) )
const _data = bufferToString(data)
let encrypted = cipher.update( let encrypted = cipher.update(
data, _data,
DEFAULT_ENCODING_TYPE, DEFAULT_ENCODING_TYPE,
DEFAULT_BUFFER_TYPE DEFAULT_BUFFER_TYPE
) )
@ -25,14 +26,19 @@ const encrypt = (
return encrypted return encrypted
} }
const decrypt = (data: string, key: Buffer, iv?: Buffer | null): string => { const decrypt = (
data: string | Buffer,
key: string | Buffer,
iv?: string | Buffer | null
): string => {
const decipher = crypto.createDecipheriv( const decipher = crypto.createDecipheriv(
DEFAULT_AES_ALGORITHM, DEFAULT_AES_ALGORITHM,
stringToBuffer(key), stringToBuffer(key),
stringToBuffer(iv || key) stringToBuffer(iv || key)
) )
const _data = bufferToString(data)
let decrypted = decipher.update( let decrypted = decipher.update(
data, _data,
DEFAULT_BUFFER_TYPE, DEFAULT_BUFFER_TYPE,
DEFAULT_ENCODING_TYPE DEFAULT_ENCODING_TYPE
) )

View File

@ -1,25 +1,23 @@
import * as crypto from "crypto" import * as crypto from "crypto"
import { DEFAULT_BUFFER_TYPE, DEFAULT_ENCODING_TYPE } from "../config" import { DEFAULT_BUFFER_TYPE, DEFAULT_ENCODING_TYPE } from "../../config"
import { readFileString, stringToBuffer } from "../util" import { stringToBuffer } from "../../util"
const encrypt = (data: string | Buffer, privOrPubKeyPath: string): string => { const encrypt = (data: string | Buffer, privateOrPublicKey: string): string => {
const key = readFileString(privOrPubKeyPath)
const buffer = stringToBuffer(data) const buffer = stringToBuffer(data)
const encrypted = crypto.publicEncrypt(key, buffer) const encrypted = crypto.publicEncrypt(privateOrPublicKey, buffer)
return encrypted.toString(DEFAULT_BUFFER_TYPE) return encrypted.toString(DEFAULT_BUFFER_TYPE)
} }
const decrypt = ( const decrypt = (
data: string | Buffer, data: string | Buffer,
privateKeyPath: string, privateKey: string,
passphrase?: string | null passphrase?: string | null
): string => { ): string => {
const key = readFileString(privateKeyPath)
const buffer = stringToBuffer(data) const buffer = stringToBuffer(data)
const decrypted = crypto.privateDecrypt( const decrypted = crypto.privateDecrypt(
{ {
key: key, key: privateKey,
passphrase: passphrase || "", passphrase: passphrase || "",
}, },
buffer buffer

21
src/crypto/crypto.ts Normal file
View File

@ -0,0 +1,21 @@
import { ICryptoProvider } from "./provider.crypto"
export class CryptoProvider {
constructor(private readonly provider: ICryptoProvider) {}
public encrypt(data: string | Buffer): string {
return this.provider.encrypt(data)
}
public decrypt(data: string): string {
return this.provider.decrypt(data)
}
private static _instance: CryptoProvider | null | undefined
public static newInstance(provider: ICryptoProvider): CryptoProvider {
if (!CryptoProvider._instance) {
CryptoProvider._instance = new CryptoProvider(provider)
}
return CryptoProvider._instance
}
}

View File

@ -1,4 +1,19 @@
import { decrypt, encrypt } from "./default-crypto" import { decrypt, encrypt } from "./core/default.crypto"
import { encrypt as e2eEncrypt, decrypt as e2eDecrypt } from "./e2e-crypto" import { encrypt as e2eEncrypt, decrypt as e2eDecrypt } from "./core/e2e.crypto"
import { CryptoProvider } from "./crypto"
import { ICryptoProvider } from "./provider.crypto"
import { DefaultCryptoProvider } from "./provider/default.provider"
import { E2ECryptoProvider } from "./provider/e2e.provider"
export { encrypt, decrypt, e2eEncrypt, e2eDecrypt } export {
encrypt,
decrypt,
e2eEncrypt,
e2eDecrypt,
// Some implementations of ICryptoProvider
ICryptoProvider,
DefaultCryptoProvider,
E2ECryptoProvider,
// Core crypto functions
CryptoProvider,
}

View File

@ -0,0 +1,4 @@
export interface ICryptoProvider {
encrypt(data: string | Buffer): string
decrypt(data: string): string
}

View File

@ -0,0 +1,33 @@
import { assertNotNullOrUndefined } from "../../util"
import { ICryptoProvider } from "../provider.crypto"
import { decrypt as dec, encrypt as enc } from "./../core/default.crypto"
export class DefaultCryptoProvider implements ICryptoProvider {
private _key: string | Buffer | null | undefined
private _iv: string | Buffer | null | undefined
constructor(
key?: string | Buffer | null,
iv?: string | Buffer | null,
keyJsonFilePath?: string | null
) {
if (keyJsonFilePath) {
const json: any = require(keyJsonFilePath)
this._key = json.key
this._iv = json.iv
} else {
this._key = key
this._iv = iv || key
}
assertNotNullOrUndefined(this._key, "key is required")
}
encrypt(data: string | Buffer): string {
return enc(data, this._key!, this._iv)
}
decrypt(data: string): string {
return dec(data, this._key!, this._iv)
}
}

View File

@ -0,0 +1,82 @@
import { e2eDecrypt, e2eEncrypt } from ".."
import {
assertNotNullOrUndefined,
isNullOrUndefinedOrEmpty,
readFileToString,
} from "../../util"
import { ICryptoProvider } from "../provider.crypto"
export class E2ECryptoProvider implements ICryptoProvider {
private privateKey: string | Buffer | null | undefined
private publicKey: string | Buffer | null | undefined
constructor(
privateKey?: string | Buffer | null | undefined,
publicKey?: string | Buffer | null | undefined,
privateKeyPath?: string | null | undefined,
publicKeyPath?: string | null | undefined,
jsonPath?: string | null | undefined
) {
let isPrivateKeyInitialized = false
let isPublicKeyInitialized = false
if (privateKeyPath) {
this.privateKey = readFileToString(privateKeyPath)
isPrivateKeyInitialized = !isNullOrUndefinedOrEmpty(this.privateKey)
}
if (publicKeyPath) {
this.publicKey = readFileToString(publicKeyPath)
isPublicKeyInitialized = !isNullOrUndefinedOrEmpty(this.publicKey)
}
if (!isPrivateKeyInitialized && !isPublicKeyInitialized && jsonPath) {
const json = require(jsonPath)
if (json.privateKey) {
this.privateKey = json.privateKey
isPrivateKeyInitialized = !isNullOrUndefinedOrEmpty(
this.privateKey
)
}
if (json.publicKey) {
this.publicKey = json.publicKey
isPublicKeyInitialized = !isNullOrUndefinedOrEmpty(
this.publicKey
)
}
if (!isPrivateKeyInitialized && json.privateKeyPath) {
this.privateKey = readFileToString(json.privateKeyPath)
isPrivateKeyInitialized = !isNullOrUndefinedOrEmpty(
this.privateKey
)
}
if (!isPublicKeyInitialized && json.publicKeyPath) {
this.publicKey = readFileToString(json.publicKeyPath)
isPublicKeyInitialized = !isNullOrUndefinedOrEmpty(
this.publicKey
)
}
}
if (!isPrivateKeyInitialized) {
this.privateKey = privateKey
}
if (!isPublicKeyInitialized) {
this.publicKey = publicKey
}
assertNotNullOrUndefined(this.privateKey, "Private key is required")
}
encrypt(data: string | Buffer): string {
const key = this.publicKey || this.privateKey
return e2eEncrypt(data, key!.toString())
}
decrypt(data: string): string {
return e2eDecrypt(data, this.privateKey!.toString())
}
}

View File

@ -2,9 +2,13 @@ import * as fs from "fs"
import * as path from "path" import * as path from "path"
import { DEFAULT_ENCODING_TYPE } from "../config" import { DEFAULT_ENCODING_TYPE } from "../config"
export const readFileStringDefaultEncoding = ( export const readFileToStringDefaultEncoding = (
relativeFilePath: string relativeFilePath: string
): string => { ): string => {
const absolutePath = path.resolve(relativeFilePath) const absolutePath = path.resolve(relativeFilePath)
return fs.readFileSync(absolutePath, DEFAULT_ENCODING_TYPE) return fs.readFileSync(absolutePath, DEFAULT_ENCODING_TYPE)
} }
export const readFileToJson = (relativeFilePath: string): any => {
return JSON.parse(readFileToStringDefaultEncoding(relativeFilePath))
}

View File

@ -1,5 +1,11 @@
import { DEFAULT_BUFFER_TYPE } from "../config" import { DEFAULT_BUFFER_TYPE, DEFAULT_ENCODING_TYPE } from "../config"
import { readFileStringDefaultEncoding } from "./file.util" import { readFileToStringDefaultEncoding, readFileToJson } from "./file.util"
import {
assertNotNullOrUndefined,
assertNotNullOrUndefinedOrEmpty,
isNullOrUndefined,
isNullOrUndefinedOrEmpty,
} from "./test.util"
const stringToBuffer = (data: string | Buffer): Buffer => { const stringToBuffer = (data: string | Buffer): Buffer => {
if (data instanceof Buffer) { if (data instanceof Buffer) {
@ -9,4 +15,21 @@ const stringToBuffer = (data: string | Buffer): Buffer => {
} }
} }
export { readFileStringDefaultEncoding as readFileString, stringToBuffer } const bufferToString = (data: string | Buffer): string => {
if (data instanceof Buffer) {
return data.toString(DEFAULT_ENCODING_TYPE)
} else {
return data
}
}
export {
readFileToStringDefaultEncoding as readFileToString,
readFileToJson,
stringToBuffer,
bufferToString,
assertNotNullOrUndefined,
assertNotNullOrUndefinedOrEmpty,
isNullOrUndefined,
isNullOrUndefinedOrEmpty,
}

22
src/util/test.util.ts Normal file
View File

@ -0,0 +1,22 @@
export const assertNotNullOrUndefined = (value: unknown, msg?: string) => {
if (isNullOrUndefined(value)) {
throw new Error(msg || "value is null or undefined")
}
}
export const assertNotNullOrUndefinedOrEmpty = (
value: unknown,
msg?: string
) => {
if (isNullOrUndefinedOrEmpty(value)) {
throw new Error(msg || "value is null or undefined or empty")
}
}
export const isNullOrUndefined = (value: unknown): boolean => {
return value === null || value === undefined
}
export const isNullOrUndefinedOrEmpty = (value: unknown): boolean => {
return isNullOrUndefined(value) || value === ""
}

3
test/crypto.test.ts Normal file
View File

@ -0,0 +1,3 @@
test("test", () => {
expect(1).toBe(1)
})

31
test/validate-ase.test.ts Normal file
View File

@ -0,0 +1,31 @@
import { readFileToString, readFileToJson } from "./../src/util"
test("test: readFileToString", () => {
const value1 = readFileToString("./dummy/ase-256-32.key")
const value2 = readFileToString("./dummy/ase-256-16.key")
expect(value1).toBe(
"56773703de5fdbe4292749c2505b6059a6698aad7881065df0818442f7d559b2"
)
expect(value2).toBe("6ef348dd9b0b4f29e47f4f1c9d7b6255")
})
test("test: readFileToString with jsonFile key-paths", () => {
const json = readFileToJson("./dummy/ase-256.keys.json")
const value1 = readFileToString(json.keyPath)
const value2 = readFileToString(json.ivPath)
expect(value1).toBe(
"56773703de5fdbe4292749c2505b6059a6698aad7881065df0818442f7d559b2"
)
expect(value2).toBe("6ef348dd9b0b4f29e47f4f1c9d7b6255")
})
test("test: readFileToString with jsonFile values", () => {
const json = readFileToJson("./dummy/ase-256.json")
const value1 = json.key
const value2 = json.iv
expect(value1).toBe(
"56773703de5fdbe4292749c2505b6059a6698aad7881065df0818442f7d559b2"
)
expect(value2).toBe("6ef348dd9b0b4f29e47f4f1c9d7b6255")
})

View File

@ -0,0 +1,30 @@
import { readFileToString, readFileToJson } from "./../src/util"
const PRIVATE_KEY_VALUE =
"-----BEGIN RSA PRIVATE KEY----- MIIJKQIBAAKCAgEA0PfjoO54+QZui0EfssxGvHOm8bhDzQIIDcH/jUvN25E28MYF VOy1CgDk2n25eakhaaK8ogmP9PfczGpLalCGfVACCXNQFc60k6zjQY8lNY6DY2NJ 4qGR5OZsA2lhXgYtptqHCvGnqqIBpfYkddKzZOjQGZvUQ3XVZvEpkDuJGvPFW/+B 4NjsgSK0bJzygnJnbJYUHezBVRQynJRIxJmCZfuhqJM2w0FLB1/8TZVn9w3OC3DX avU7DfEGOlJb9evRrvdVGag14Pcy6SXoZ1jFscRJuXyWy1mLooTx6osj7V6VYhnK ZJrlRTAUxnGZLuBdqYztU7SNhZfmuCSIS+nduqrdM9P3NPqgOqmIMTps0fcQg0DU 5CFdWIy/FecErg1YaAhbEOqQO4QQlTmMDFFJ63Y9rE3s9W5k30ZP8Ydxa8UT3su3 6ePzUGzWXUu2M8ffc8nnfiAvh1XgJiBqDJkmVEG3GzmDL0BfDLqMnhBJ2UaQPXK9 w52masDgvHvyMEZoBaFBHtVICl4Jek6O9KSz1uWcR/+C72fBEU4iA/h4rdH4PVMT P99YhN+Zd4w9UM2tITafCM3RdNbKZ3f+oXfFCjqNKiJI/VSLkErOKQnw/AiGT8dp mmFGl+DazUsYNEP3ScFUAI+ZqsUyh5q9SoCnYmcpuMbXLRLVYQJQASkYwBkCAwEA AQKCAgEAljZ7NIX4A9pxwDxSEu7wsirYiM2SP14EeA9KKkGhewIJ4k4WKxP0l3pJ CB50I1ZGpG7M9hW1m7xIxdBq4cN8ybunmy9/FA4AlF8lBU1my/bjz5/hzr2h6E8S uekYisQaMOTJSj6n7eqDEM+9Kc0KE7GfValEjjG380XMQNfrGKq+5gDnVb/Z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jg5AgKgQnIvxjQTmTOttniokCECg3P pTnSGaIPt739ZVVTh+ENOvxMGoh5JGJlwAuRfS+98NHSrFDh+UWW9Nin4aPf -----END RSA PRIVATE KEY-----"
const PUBLIC_KEY_VALUE =
"-----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0PfjoO54+QZui0EfssxG vHOm8bhDzQIIDcH/jUvN25E28MYFVOy1CgDk2n25eakhaaK8ogmP9PfczGpLalCG fVACCXNQFc60k6zjQY8lNY6DY2NJ4qGR5OZsA2lhXgYtptqHCvGnqqIBpfYkddKz ZOjQGZvUQ3XVZvEpkDuJGvPFW/+B4NjsgSK0bJzygnJnbJYUHezBVRQynJRIxJmC ZfuhqJM2w0FLB1/8TZVn9w3OC3DXavU7DfEGOlJb9evRrvdVGag14Pcy6SXoZ1jF scRJuXyWy1mLooTx6osj7V6VYhnKZJrlRTAUxnGZLuBdqYztU7SNhZfmuCSIS+nd uqrdM9P3NPqgOqmIMTps0fcQg0DU5CFdWIy/FecErg1YaAhbEOqQO4QQlTmMDFFJ 63Y9rE3s9W5k30ZP8Ydxa8UT3su36ePzUGzWXUu2M8ffc8nnfiAvh1XgJiBqDJkm VEG3GzmDL0BfDLqMnhBJ2UaQPXK9w52masDgvHvyMEZoBaFBHtVICl4Jek6O9KSz 1uWcR/+C72fBEU4iA/h4rdH4PVMTP99YhN+Zd4w9UM2tITafCM3RdNbKZ3f+oXfF CjqNKiJI/VSLkErOKQnw/AiGT8dpmmFGl+DazUsYNEP3ScFUAI+ZqsUyh5q9SoCn YmcpuMbXLRLVYQJQASkYwBkCAwEAAQ== -----END PUBLIC KEY-----"
test("test: readFileToString with private and public key files", () => {
const value1 = readFileToString("./dummy/private-key.pem")
const value2 = readFileToString("./dummy/public-key.pem")
expect(value1).toBe(PRIVATE_KEY_VALUE)
expect(value2).toBe(PUBLIC_KEY_VALUE)
})
test("test: readFileToString with private and public jsonFile key-paths", () => {
const json = readFileToJson("./dummy/private-public.keys.json")
const value1 = readFileToString(json.privateKeyPath)
const value2 = readFileToString(json.publicKeyPath)
expect(value1).toBe(PRIVATE_KEY_VALUE)
expect(value2).toBe(PUBLIC_KEY_VALUE)
})
test("test: readFileToString with private and public jsonFile values", () => {
const json = readFileToJson("./dummy/private-public.json")
const value1 = json.privateKey
const value2 = json.publicKey
expect(value1).toBe(PRIVATE_KEY_VALUE)
expect(value2).toBe(PUBLIC_KEY_VALUE)
})