@@ -7,13 +7,13 @@ import {
77import { Buffer } from 'node:buffer' ;
88
99class CipherService {
10+ private hash ( value : string ) : Uint8Array {
11+ return createHash ( 'sha256' ) . update ( value ) . digest ( ) ;
12+ }
13+
1014 public encrypt ( key : string , message : string ) : string {
1115 const iv = randomBytes ( 16 ) ;
12- const cipher = createCipheriv (
13- 'aes-256-cfb' ,
14- this . hash ( key ) ,
15- iv ,
16- ) . setAutoPadding ( false ) ;
16+ const cipher = createCipheriv ( 'aes-256-cfb' , this . hash ( key ) , iv ) . setAutoPadding ( false ) ;
1717 return Buffer . concat ( [
1818 iv ,
1919 cipher . update ( message , 'utf8' ) ,
@@ -24,21 +24,13 @@ class CipherService {
2424 public decrypt ( key : string , message : string ) : string {
2525 const cipherBytes = Buffer . from ( message , 'base64' ) ;
2626 const iv = cipherBytes . subarray ( 0 , 16 ) ;
27- const decipher = createDecipheriv (
28- 'aes-256-cfb' ,
29- this . hash ( key ) ,
30- iv ,
31- ) . setAutoPadding ( false ) ;
27+ const decipher = createDecipheriv ( 'aes-256-cfb' , this . hash ( key ) , iv ) . setAutoPadding ( false ) ;
3228
3329 return Buffer . concat ( [
3430 decipher . update ( cipherBytes . subarray ( 16 , cipherBytes . length ) ) ,
3531 decipher . final ( ) ,
3632 ] ) . toString ( ) ;
3733 }
38-
39- private hash ( value : string ) : Uint8Array {
40- return createHash ( 'sha256' ) . update ( value ) . digest ( ) ;
41- }
4234}
4335
4436export default CipherService ;
0 commit comments