Skip to content

Commit

Permalink
add signature function (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
grendyirwandi authored Feb 16, 2022
1 parent ba2f417 commit ab00b05
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { AES256Encryption } from './aes256-encryption';
export { RSAEncryption } from './rsa-encryption';
export { Signature } from './signature';
23 changes: 23 additions & 0 deletions src/signature.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions src/types/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Encoding = 'base64' | 'hex';
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './bit-length';
export * from './cipher-aes256-mode';
export * from './encoding';
export * from './sign-algorithm';
1 change: 1 addition & 0 deletions src/types/sign-algorithm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type SignAlgorithm = 'SHA1';
3 changes: 3 additions & 0 deletions test/Signature.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {Signature} from '../src'
test('verify signature', () => {
});

0 comments on commit ab00b05

Please sign in to comment.