From ab00b05342b1e4f5ed1b4d178ffee198c9192b0a Mon Sep 17 00:00:00 2001 From: Grendy Irwandi Date: Wed, 16 Feb 2022 07:20:30 +0700 Subject: [PATCH] add signature function (#82) --- src/index.ts | 1 + src/signature.ts | 23 +++++++++++++++++++++++ src/types/encoding.ts | 1 + src/types/index.ts | 2 ++ src/types/sign-algorithm.ts | 1 + test/Signature.test.ts | 3 +++ 6 files changed, 31 insertions(+) create mode 100644 src/signature.ts create mode 100644 src/types/encoding.ts create mode 100644 src/types/sign-algorithm.ts create mode 100644 test/Signature.test.ts diff --git a/src/index.ts b/src/index.ts index a0b457a..89c04e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { AES256Encryption } from './aes256-encryption'; export { RSAEncryption } from './rsa-encryption'; +export { Signature } from './signature'; diff --git a/src/signature.ts b/src/signature.ts new file mode 100644 index 0000000..5b0c26a --- /dev/null +++ b/src/signature.ts @@ -0,0 +1,23 @@ +import { createHash } from 'crypto'; +import { Encoding, SignAlgorithm } from './types'; + +export class Signature { + private algorithm: SignAlgorithm; + private encoding: Encoding; + constructor(algorithm: SignAlgorithm, encoding?: Encoding) { + this.algorithm = algorithm; + this.encoding = encoding ? encoding : 'base64'; + } + + generate(data: string) { + const hash = createHash(this.algorithm); + hash.update(data); + const createSignature = hash.digest().toString(this.encoding); + return createSignature; + } + + verify(data: string, signature: string) { + const createSignature = this.generate(data); + return createSignature === signature; + } +} diff --git a/src/types/encoding.ts b/src/types/encoding.ts new file mode 100644 index 0000000..460219b --- /dev/null +++ b/src/types/encoding.ts @@ -0,0 +1 @@ +export type Encoding = 'base64' | 'hex'; diff --git a/src/types/index.ts b/src/types/index.ts index cd58673..3e44666 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,2 +1,4 @@ export * from './bit-length'; export * from './cipher-aes256-mode'; +export * from './encoding'; +export * from './sign-algorithm'; diff --git a/src/types/sign-algorithm.ts b/src/types/sign-algorithm.ts new file mode 100644 index 0000000..0a50727 --- /dev/null +++ b/src/types/sign-algorithm.ts @@ -0,0 +1 @@ +export type SignAlgorithm = 'SHA1'; diff --git a/test/Signature.test.ts b/test/Signature.test.ts new file mode 100644 index 0000000..74ccc24 --- /dev/null +++ b/test/Signature.test.ts @@ -0,0 +1,3 @@ +import {Signature} from '../src' +test('verify signature', () => { +}); \ No newline at end of file