@@ -22,8 +22,8 @@ export default class SymmetricKey extends BigNumber {
2222 * @example
2323 * const symmetricKey = SymmetricKey.fromRandom();
2424 */
25- static fromRandom ( ) : SymmetricKey {
26- return new SymmetricKey ( Random ( 32 ) ) ;
25+ static fromRandom ( ) : SymmetricKey {
26+ return new SymmetricKey ( Random ( 32 ) )
2727 }
2828
2929 /**
@@ -40,20 +40,20 @@ export default class SymmetricKey extends BigNumber {
4040 * const key = new SymmetricKey(1234);
4141 * const encryptedMessage = key.encrypt('plainText', 'utf8');
4242 */
43- encrypt ( msg : number [ ] | string , enc ?: 'hex' ) : string | number [ ] {
44- const iv = Random ( 32 ) ;
45- msg = toArray ( msg , enc ) ;
46- const keyBytes = this . toArray ( 'be' , 32 ) ;
47- const { result, authenticationTag } = AESGCM ( msg , [ ] , iv , keyBytes ) ;
48- const totalLength = iv . length + result . length + authenticationTag . length ;
49- const combined = new Array ( totalLength ) ;
50- let offset = 0 ;
43+ encrypt ( msg : number [ ] | string , enc ?: 'hex' ) : string | number [ ] {
44+ const iv = Random ( 32 )
45+ msg = toArray ( msg , enc )
46+ const keyBytes = this . toArray ( 'be' , 32 )
47+ const { result, authenticationTag } = AESGCM ( msg , [ ] , iv , keyBytes )
48+ const totalLength = iv . length + result . length + authenticationTag . length
49+ const combined = new Array ( totalLength )
50+ let offset = 0
5151 for ( const chunk of [ iv , result , authenticationTag ] ) {
5252 for ( let i = 0 ; i < chunk . length ; i ++ ) {
53- combined [ offset ++ ] = chunk [ i ] ;
53+ combined [ offset ++ ] = chunk [ i ]
5454 }
5555 }
56- return encode ( combined , enc ) ;
56+ return encode ( combined , enc )
5757 }
5858
5959 /**
@@ -72,35 +72,35 @@ export default class SymmetricKey extends BigNumber {
7272 *
7373 * @throws {Error } Will throw an error if the decryption fails, likely due to message tampering or incorrect decryption key.
7474 */
75- decrypt ( msg : number [ ] | string , enc ?: 'hex' | 'utf8' ) : string | number [ ] {
76- msg = toArray ( msg , enc ) ;
75+ decrypt ( msg : number [ ] | string , enc ?: 'hex' | 'utf8' ) : string | number [ ] {
76+ msg = toArray ( msg , enc )
7777
7878 if ( msg . length < 48 ) {
79- throw new Error ( 'Ciphertext too short' ) ;
79+ throw new Error ( 'Ciphertext too short' )
8080 }
8181
82- const ivLength = 32 ;
83- const tagLength = 16 ;
82+ const ivLength = 32
83+ const tagLength = 16
8484
85- const iv = msg . slice ( 0 , ivLength ) ;
86- const tagStart = msg . length - tagLength ;
87- const ciphertext = msg . slice ( ivLength , tagStart ) ;
88- const messageTag = msg . slice ( tagStart ) ;
85+ const iv = msg . slice ( 0 , ivLength )
86+ const tagStart = msg . length - tagLength
87+ const ciphertext = msg . slice ( ivLength , tagStart )
88+ const messageTag = msg . slice ( tagStart )
8989
9090 if ( tagStart < ivLength ) {
91- throw new Error ( 'Malformed ciphertext' ) ;
91+ throw new Error ( 'Malformed ciphertext' )
9292 }
9393
9494 const result = AESGCMDecrypt (
9595 ciphertext ,
9696 [ ] ,
9797 iv ,
9898 messageTag ,
99- this . toArray ( 'be' , 32 ) ,
100- ) ;
99+ this . toArray ( 'be' , 32 )
100+ )
101101 if ( result === null ) {
102- throw new Error ( 'Decryption failed!' ) ;
102+ throw new Error ( 'Decryption failed!' )
103103 }
104- return encode ( result , enc ) ;
104+ return encode ( result , enc )
105105 }
106106}
0 commit comments