Refactoring name

This commit is contained in:
Sambo Chea 2022-02-14 19:05:24 +07:00
parent 9d89ac0b1e
commit 27f9320bda
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
19 changed files with 64 additions and 76 deletions

View File

@ -1,10 +1,10 @@
/// <reference types="node" />
import { ICryptoProvider } from "./provider.crypto"
export declare class CryptoProvider {
import { CryptoProvider } from "./provider.crypto"
export declare class Crypto {
private readonly provider
constructor(provider: ICryptoProvider)
constructor(provider: CryptoProvider)
encrypt(data: string | Buffer): string
decrypt(data: string): string
private static _instance
static newInstance(provider: ICryptoProvider): CryptoProvider
static newInstance(provider: CryptoProvider): Crypto
}

22
dist/crypto/crypto.js vendored
View File

@ -1,23 +1,23 @@
"use strict"
Object.defineProperty(exports, "__esModule", { value: true })
exports.CryptoProvider = void 0
var CryptoProvider = /** @class */ (function () {
function CryptoProvider(provider) {
exports.Crypto = void 0
var Crypto = /** @class */ (function () {
function Crypto(provider) {
this.provider = provider
}
CryptoProvider.prototype.encrypt = function (data) {
Crypto.prototype.encrypt = function (data) {
return this.provider.encrypt(data)
}
CryptoProvider.prototype.decrypt = function (data) {
Crypto.prototype.decrypt = function (data) {
return this.provider.decrypt(data)
}
CryptoProvider.newInstance = function (provider) {
if (!CryptoProvider._instance) {
CryptoProvider._instance = new CryptoProvider(provider)
Crypto.newInstance = function (provider) {
if (!Crypto._instance) {
Crypto._instance = new Crypto(provider)
}
return CryptoProvider._instance
return Crypto._instance
}
return CryptoProvider
return Crypto
})()
exports.CryptoProvider = CryptoProvider
exports.Crypto = Crypto
//# sourceMappingURL=crypto.js.map

View File

@ -1 +1 @@
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto/crypto.ts"],"names":[],"mappings":";;;AAEA;IACI,wBAA6B,QAAyB;QAAzB,aAAQ,GAAR,QAAQ,CAAiB;IAAG,CAAC;IAEnD,gCAAO,GAAd,UAAe,IAAqB;QAChC,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACtC,CAAC;IAEM,gCAAO,GAAd,UAAe,IAAY;QACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACtC,CAAC;IAGa,0BAAW,GAAzB,UAA0B,QAAyB;QAC/C,IAAI,CAAC,cAAc,CAAC,SAAS,EAAE;YAC3B,cAAc,CAAC,SAAS,GAAG,IAAI,cAAc,CAAC,QAAQ,CAAC,CAAA;SAC1D;QACD,OAAO,cAAc,CAAC,SAAS,CAAA;IACnC,CAAC;IACL,qBAAC;AAAD,CAAC,AAlBD,IAkBC;AAlBY,wCAAc"}
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto/crypto.ts"],"names":[],"mappings":";;;AAEA;IACI,gBAA6B,QAAwB;QAAxB,aAAQ,GAAR,QAAQ,CAAgB;IAAG,CAAC;IAElD,wBAAO,GAAd,UAAe,IAAqB;QAChC,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACtC,CAAC;IAEM,wBAAO,GAAd,UAAe,IAAY;QACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACtC,CAAC;IAGa,kBAAW,GAAzB,UAA0B,QAAwB;QAC9C,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;YACnB,MAAM,CAAC,SAAS,GAAG,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAA;SAC1C;QACD,OAAO,MAAM,CAAC,SAAS,CAAA;IAC3B,CAAC;IACL,aAAC;AAAD,CAAC,AAlBD,IAkBC;AAlBY,wBAAM"}

View File

@ -1,7 +1,7 @@
import { decrypt, encrypt } from "./core/default.crypto"
import { encrypt as e2eEncrypt, decrypt as e2eDecrypt } from "./core/e2e.crypto"
import { CryptoProvider } from "./crypto"
import { ICryptoProvider } from "./provider.crypto"
import { Crypto } from "./crypto"
import { CryptoProvider } from "./provider.crypto"
import { DefaultCryptoProvider } from "./provider/default.provider"
import { E2ECryptoProvider } from "./provider/e2e.provider"
export {
@ -9,8 +9,8 @@ export {
decrypt,
e2eEncrypt,
e2eDecrypt,
ICryptoProvider,
CryptoProvider,
DefaultCryptoProvider,
E2ECryptoProvider,
CryptoProvider,
Crypto,
}

View File

@ -1,6 +1,6 @@
"use strict"
Object.defineProperty(exports, "__esModule", { value: true })
exports.CryptoProvider =
exports.Crypto =
exports.E2ECryptoProvider =
exports.DefaultCryptoProvider =
exports.e2eDecrypt =
@ -35,10 +35,10 @@ Object.defineProperty(exports, "e2eDecrypt", {
},
})
var crypto_1 = require("./crypto")
Object.defineProperty(exports, "CryptoProvider", {
Object.defineProperty(exports, "Crypto", {
enumerable: true,
get: function () {
return crypto_1.CryptoProvider
return crypto_1.Crypto
},
})
var default_provider_1 = require("./provider/default.provider")

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/crypto/index.ts"],"names":[],"mappings":";;;AAAA,wDAAwD;AASpD,wFATK,wBAAO,OASL;AADP,wFARc,wBAAO,OAQd;AAPX,gDAAgF;AAS5E,2FATgB,oBAAU,OAShB;AACV,2FAVuC,oBAAU,OAUvC;AATd,mCAAyC;AAerC,+FAfK,uBAAc,OAeL;AAblB,gEAAmE;AAU/D,sGAVK,wCAAqB,OAUL;AATzB,wDAA2D;AAUvD,kGAVK,gCAAiB,OAUL"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/crypto/index.ts"],"names":[],"mappings":";;;AAAA,wDAAwD;AASpD,wFATK,wBAAO,OASL;AADP,wFARc,wBAAO,OAQd;AAPX,gDAAgF;AAS5E,2FATgB,oBAAU,OAShB;AACV,2FAVuC,oBAAU,OAUvC;AATd,mCAAiC;AAe7B,uFAfK,eAAM,OAeL;AAbV,gEAAmE;AAU/D,sGAVK,wCAAqB,OAUL;AATzB,wDAA2D;AAUvD,kGAVK,gCAAiB,OAUL"}

View File

@ -1,5 +1,5 @@
/// <reference types="node" />
export interface ICryptoProvider {
export interface CryptoProvider {
encrypt(data: string | Buffer): string
decrypt(data: string): string
}

View File

@ -1,12 +1,12 @@
/// <reference types="node" />
import { ICryptoProvider } from "../provider.crypto"
import { CryptoProvider } from "../provider.crypto"
interface DefaultCryptoProviderOptions {
key?: string | Buffer | null | undefined
iv?: string | Buffer | null | undefined
keyiVPath?: string | null | undefined
jsonPath?: string | null | undefined
}
export declare class DefaultCryptoProvider implements ICryptoProvider {
export declare class DefaultCryptoProvider implements CryptoProvider {
private _key
private _iv
constructor(options: DefaultCryptoProviderOptions)

View File

@ -1,5 +1,5 @@
/// <reference types="node" />
import { ICryptoProvider } from "../provider.crypto"
import { CryptoProvider } from "../provider.crypto"
interface E2ECryptoProviderOptions {
privateKey?: string | Buffer | null | undefined
publicKey?: string | Buffer | null | undefined
@ -7,7 +7,7 @@ interface E2ECryptoProviderOptions {
publicKeyPath?: string | null | undefined
jsonPath?: string | null | undefined
}
export declare class E2ECryptoProvider implements ICryptoProvider {
export declare class E2ECryptoProvider implements CryptoProvider {
private privateKey
private publicKey
constructor(options: E2ECryptoProviderOptions)

8
dist/index.d.ts vendored
View File

@ -1,6 +1,2 @@
import {
CryptoProvider,
DefaultCryptoProvider,
E2ECryptoProvider,
} from "./crypto"
export { CryptoProvider, DefaultCryptoProvider, E2ECryptoProvider }
import { Crypto, DefaultCryptoProvider, E2ECryptoProvider } from "./crypto"
export { Crypto, DefaultCryptoProvider, E2ECryptoProvider }

6
dist/index.js vendored
View File

@ -2,13 +2,13 @@
Object.defineProperty(exports, "__esModule", { value: true })
exports.E2ECryptoProvider =
exports.DefaultCryptoProvider =
exports.CryptoProvider =
exports.Crypto =
void 0
var crypto_1 = require("./crypto")
Object.defineProperty(exports, "CryptoProvider", {
Object.defineProperty(exports, "Crypto", {
enumerable: true,
get: function () {
return crypto_1.CryptoProvider
return crypto_1.Crypto
},
})
Object.defineProperty(exports, "DefaultCryptoProvider", {

2
dist/index.js.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAIiB;AAER,+FALL,uBAAc,OAKK;AAAE,sGAJrB,8BAAqB,OAIqB;AAAE,kGAH5C,0BAAiB,OAG4C"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAIiB;AAER,uFALL,eAAM,OAKK;AAAE,sGAJb,8BAAqB,OAIa;AAAE,kGAHpC,0BAAiB,OAGoC"}

View File

@ -1,7 +1,7 @@
import { ICryptoProvider } from "./provider.crypto"
import { CryptoProvider } from "./provider.crypto"
export class CryptoProvider {
constructor(private readonly provider: ICryptoProvider) {}
export class Crypto {
constructor(private readonly provider: CryptoProvider) {}
public encrypt(data: string | Buffer): string {
return this.provider.encrypt(data)
@ -11,11 +11,11 @@ export class CryptoProvider {
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)
private static _instance: Crypto | null | undefined
public static newInstance(provider: CryptoProvider): Crypto {
if (!Crypto._instance) {
Crypto._instance = new Crypto(provider)
}
return CryptoProvider._instance
return Crypto._instance
}
}

View File

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

View File

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

View File

@ -3,7 +3,7 @@ import {
readFileToJson,
readFileToString,
} from "../../util"
import { ICryptoProvider } from "../provider.crypto"
import { CryptoProvider } from "../provider.crypto"
import { decrypt as dec, encrypt as enc } from "./../core/default.crypto"
interface DefaultCryptoProviderOptions {
@ -13,7 +13,7 @@ interface DefaultCryptoProviderOptions {
jsonPath?: string | null | undefined
}
export class DefaultCryptoProvider implements ICryptoProvider {
export class DefaultCryptoProvider implements CryptoProvider {
private _key: string | Buffer | null | undefined
private _iv: string | Buffer | null | undefined

View File

@ -5,7 +5,7 @@ import {
readFileToJson,
readFileToString,
} from "../../util"
import { ICryptoProvider } from "../provider.crypto"
import { CryptoProvider } from "../provider.crypto"
interface E2ECryptoProviderOptions {
privateKey?: string | Buffer | null | undefined
@ -15,7 +15,7 @@ interface E2ECryptoProviderOptions {
jsonPath?: string | null | undefined
}
export class E2ECryptoProvider implements ICryptoProvider {
export class E2ECryptoProvider implements CryptoProvider {
private privateKey: string | Buffer | null | undefined
private publicKey: string | Buffer | null | undefined

View File

@ -1,7 +1,3 @@
import {
CryptoProvider,
DefaultCryptoProvider,
E2ECryptoProvider,
} from "./crypto"
import { Crypto, DefaultCryptoProvider, E2ECryptoProvider } from "./crypto"
export { CryptoProvider, DefaultCryptoProvider, E2ECryptoProvider }
export { Crypto, DefaultCryptoProvider, E2ECryptoProvider }

View File

@ -1,8 +1,4 @@
import {
CryptoProvider,
DefaultCryptoProvider,
E2ECryptoProvider,
} from "../src"
import { Crypto, DefaultCryptoProvider, E2ECryptoProvider } from "../src"
// DefaultCryptoProvider
const key = "67rKmuc6DiDukE0jsUP421Eizo4CreaL6Q7Pg/NmH/s="
@ -24,7 +20,7 @@ const publicKeyPath = "./dummy/public-key.pem"
const TEXT = `Hello World and Then...!`
test("test: Default Encryption From Values", () => {
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new DefaultCryptoProvider({
key: key,
iv: iv,
@ -38,7 +34,7 @@ test("test: Default Encryption From Values", () => {
test("test: Default Encryption with Hello World", () => {
const data = "Hello World"
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new DefaultCryptoProvider({
key: key,
iv: iv,
@ -53,7 +49,7 @@ test("test: Default Encryption with Hello World", () => {
})
test("test: Default Encryption From JsonFile Values", () => {
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new DefaultCryptoProvider({
keyiVPath: keyJsonFileValuesDefault,
})
@ -65,7 +61,7 @@ test("test: Default Encryption From JsonFile Values", () => {
})
test("test: Default Encryption From JsonFile Keys Path", () => {
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new DefaultCryptoProvider({
jsonPath: keyJsonFileKeysDefault,
})
@ -77,7 +73,7 @@ test("test: Default Encryption From JsonFile Keys Path", () => {
})
test("test: E2E Encryption From Values", () => {
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new E2ECryptoProvider({
publicKey: PUBLIC_KEY_VALUE,
privateKey: PRIVATE_KEY_VALUE,
@ -90,7 +86,7 @@ test("test: E2E Encryption From Values", () => {
})
test("test: E2E Encryption From Files Values", () => {
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new E2ECryptoProvider({
privateKeyPath: privateKeyPath,
publicKeyPath: publicKeyPath,
@ -103,7 +99,7 @@ test("test: E2E Encryption From Files Values", () => {
})
test("test: E2E Encryption From JsonFile Values", () => {
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new E2ECryptoProvider({
jsonPath: privatePublicJsonFileValuesDefault,
})
@ -115,7 +111,7 @@ test("test: E2E Encryption From JsonFile Values", () => {
})
test("test: E2E Encryption From JsonFile Keys Path", () => {
const provider = CryptoProvider.newInstance(
const provider = Crypto.newInstance(
new E2ECryptoProvider({
jsonPath: privatePublicJsonFileKeysDefault,
})