1
- module . exports = class {
2
- static _c = require ( 'node:crypto' )
3
- static _dCrypt = { h : { alg : 'RSA-RIPEMD160' } , c : { alg : 'chacha20-poly1305' , s : { k : 32 , iv : 12 } } }
4
- static _dEnc = 'base64url'
5
- static _dbobj = ( ) => ( { _a : '' , _b : '' , _c : '' , _d : '' , _e : '' } )
6
- static _h = ( ) => this . _c . createHash ( this . _dCrypt . h . alg )
7
- static _enc = ( x , y ) => this . _c . createCipheriv ( this . _dCrypt . c . alg , x , y )
8
- static _dec = ( x , y ) => this . _c . createDecipheriv ( this . _dCrypt . c . alg , x , y )
9
- static _sttd ( a , b , _e = new Array ( 0 ) ) {
10
- Object . keys ( this . _dbobj ( ) ) . forEach ( ( k ) => { if ( ! new Object ( JSON . parse ( Buffer . from ( a , b ) . toString ( 'utf-8' ) ) ) . hasOwnProperty ( k ) ) _e . push ( `Missing ${ k } value` ) } )
11
- return ( _e . length > 0 ) ? _e . forEach ( console . error ) : JSON . parse ( Buffer . from ( a , b ) . toString ( 'utf-8' ) ) ;
12
- }
13
- static e ( d ) {
1
+ const crypto = require ( 'node:crypto' ) ;
2
+ const assert = require ( 'node:assert/strict' ) ;
3
+
4
+ const cryptoDefaults = {
5
+ hashAlgorithm : 'RSA-RIPEMD160' ,
6
+ crypt : crypto . getCipherInfo ( "chacha20-poly1305" ) ,
7
+ encoding : 'base64url' ,
8
+ } ;
9
+
10
+ class CCrypto {
11
+ static encrypt ( inputData ) {
14
12
const
15
- _d = this . _dbobj ,
16
- _in = Buffer . from ( d ) ,
17
- _giv = this . _c . randomBytes ( this . _dCrypt . c . s . iv ) ,
18
- _k = this . _c . randomBytes ( this . _dCrypt . c . s . k )
19
- _d . _c = this . _h ( ) . update ( _in ) . digest ( this . _dEnc )
20
- _d . _a = _giv . toString ( this . _dEnc )
21
- _d . _b = _k . toString ( this . _dEnc )
22
- const _encdata = this . _enc ( _k , _giv ) . update ( _in )
23
- _d . _d = this . _h ( ) . update ( _encdata ) . digest ( this . _dEnc )
24
- _d . _e = _encdata . toString ( this . _dEnc )
25
- return Buffer . from ( JSON . stringify ( _d ) , 'utf8' ) . toString ( this . _dEnc )
13
+ data = { iv : '' , key : '' , prehash : '' , posthash : '' , data : '' } ,
14
+ input = Buffer . from ( inputData ) ,
15
+ generatedIV = crypto . randomBytes ( cryptoDefaults . crypt . ivLength ) ,
16
+ key = crypto . randomBytes ( cryptoDefaults . crypt . keyLength )
17
+ data . prehash = crypto
18
+ . createHash ( cryptoDefaults . hashAlgorithm )
19
+ . update ( input )
20
+ . digest ( cryptoDefaults . encoding )
21
+ data . iv = generatedIV . toString ( cryptoDefaults . encoding )
22
+ data . key = key . toString ( cryptoDefaults . encoding )
23
+ const encdata = crypto
24
+ . createCipheriv ( cryptoDefaults . crypt . name , key , generatedIV )
25
+ . update ( input )
26
+ data . posthash = crypto
27
+ . createHash ( cryptoDefaults . hashAlgorithm )
28
+ . update ( encdata )
29
+ . digest ( cryptoDefaults . encoding )
30
+ data . data = encdata . toString ( cryptoDefaults . encoding )
31
+ return Buffer . from ( JSON . stringify ( data ) , 'utf8' ) . toString ( cryptoDefaults . encoding )
26
32
}
27
- static d ( d ) {
28
- const _d = this . _sttd ( d , this . _dEnc )
29
- if ( ! _d ) return ;
30
- const _fd = Buffer . from ( _d . _e , this . _dEnc )
31
- const _psvh = this . _h ( ) . update ( _fd ) . digest ( this . _dEnc ) ;
32
- if ( ! ( _d . _d == _psvh ) ) throw new TypeError ( "The Post-Verification Checksum failed to resolve to the provided data" )
33
+ static decrypt ( inputData ) {
34
+ const data = JSON . parse ( Buffer . from ( inputData , cryptoDefaults . encoding ) . toString ( 'utf-8' ) )
35
+ for ( const key of Object . keys ( { iv : '' , key : '' , prehash : '' , posthash : '' , data : '' } ) ) {
36
+ if ( ! Object . hasOwn ( data , key ) ) assert . fail ( `Missing ${ key } value` )
37
+ }
38
+ const formattedData = Buffer . from ( data . data , cryptoDefaults . encoding )
39
+ const postVerificationHash = crypto
40
+ . createHash ( cryptoDefaults . hashAlgorithm )
41
+ . update ( formattedData )
42
+ . digest ( cryptoDefaults . encoding ) ;
43
+ assert . equal ( data . posthash , postVerificationHash ) ;
33
44
const
34
- _giv = Buffer . from ( _d . _a , this . _dEnc ) ,
35
- _k = Buffer . from ( _d . _b , this . _dEnc ) ,
36
- _outData = this . _dec ( _k , _giv ) . update ( _fd ) ,
37
- _pevh = this . _h ( ) . update ( _outData ) . digest ( this . _dEnc ) ;
38
- if ( ! ( _d . _c == _pevh ) ) throw new TypeError ( "The Pre-Verification Checksum failed to resolve to the provided data" )
39
- const _out = _outData . toString ( 'utf-8' )
40
- return _out
45
+ generatedIV = Buffer . from ( data . iv , cryptoDefaults . encoding ) ,
46
+ key = Buffer . from ( data . key , cryptoDefaults . encoding ) ,
47
+ outData = crypto
48
+ . createDecipheriv ( cryptoDefaults . crypt . name , key , generatedIV )
49
+ . update ( formattedData ) ,
50
+ preVerificationHash = crypto
51
+ . createHash ( cryptoDefaults . hashAlgorithm )
52
+ . update ( outData )
53
+ . digest ( cryptoDefaults . encoding ) ;
54
+ assert . equal ( data . prehash , preVerificationHash ) ;
55
+ return outData . toString ( 'utf-8' )
41
56
}
42
- }
57
+ }
58
+
59
+ module . exports = CCrypto
0 commit comments