-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (59 loc) · 2.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
const { createCipheriv, randomBytes } = require( 'node:crypto' );
const { kmac256 } = require( 'js-sha3' );
const rndBytes = ( length, bytes = false ) => {
return new Promise( ( resolve, reject ) => {
randomBytes( length, ( err, buff ) => {
if ( err ) { reject( err ) };
if ( !bytes ) resolve( buff );
resolve( new Uint8Array( buff ) );
} );
} );
};
const getHmac = ( data, key, customization = '', bytes = false ) => {
const arr = new Uint8Array( kmac256.array( key, data, 512, customization ) );
if ( bytes ) return arr;
return Buffer.from( arr );
};
const encrypt = async ( data, key, hashKey = undefined, customization = '', bytes = false ) => {
try {
const iv = await rndBytes( 16 );
const cipher = createCipheriv( 'aes-256-ctr', key, iv );
let final = Buffer.concat( [cipher.update( data ), cipher.final(), iv] );
if ( !hashKey ) {
if ( !bytes ) return final;
return new Uint8Array( final );
}
const hmac = getHmac( final, hashKey, customization );
final = Buffer.concat( [final, hmac] );
if ( !bytes ) return final;
return new Uint8Array( final );
} catch {
throw new Error ( 'Invalid data' );
}
};
const decrypt = async ( data, key, hashKey = undefined, customization = '', bytes = false ) => {
try {
data = new Uint8Array( data );
if ( hashKey ) {
const mac = Buffer.from( data.slice( -64 ) ).toString( 'base64' );
data = data.slice( 0, -64 );
const realMac = getHmac( data, hashKey, customization ).toString( 'base64' );
if ( realMac !== mac ) throw new Error ( 'Invalid data' );
}
const iv = data.slice( -16 );
data = data.slice( 0, -16 )
const cipher = createCipheriv( 'aes-256-ctr', key, iv );
const buff = Buffer.concat( [cipher.update( data ), cipher.final()] );
if ( !bytes ) return buff;
return new Uint8Array( buff );
} catch {
throw new Error ( 'Invalid data' );
}
};
module.exports = {
rndBytes,
getHmac,
encrypt,
decrypt
};