Task: Completed for cryptojs and tests functions and validate keys

This commit is contained in:
2022-02-14 12:30:32 +07:00
parent 9d2a43feb9
commit fd27e6f9dc
23 changed files with 324 additions and 24 deletions

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())
}
}