CryptoAdapter

2  

Exports: Instance

This is a adapter to centralize the usage of different crypto modules

8  
9  
10  class CryptoAdapter
11    
12  
13  

setModule

CryptoAdapter.setModule( tmpl, data )

The the crypto module and set the method hmacSha256

Params
[cryptoModule=`crypto`] String The module name of the used crypto module. Possible modules are the node internal "crypto" or the browser compatible "crypto" module
Returns
CryptoAdapter A self reference
API
private
24  
25    setModule: ( cryptoModule = "crypto" )->
26      @crypto = require( cryptoModule )
27      switch cryptoModule
28        when "crypto-js"
29          @hmacSha256 = @_hmacSha256_CryptoJs
30        else
31          @hmacSha256 = @_hmacSha256_Crypto
32      return @
33    
34  
35  

hmacSha256

CryptoAdapter.hmacSha256( secret, val[, secinp, out] )

calculate a hmac shah 256 hash

Params
secret String The secret to generate the hash
val String The value to generate the hash of
[secinp="hex"] String The input encoding of the secret. ( Enum: hex, base64, utf8 )
[out="hex"] String The output encoding of the generated hash. ( Enum: hex, base64, utf8 )
Returns
String The generated hash
API
private

This is a virtual method that is generated by setModule based on the defined crypto module

_hmacSha256_Crypto

CryptoAdapter._hmacSha256_Crypto( secret, val[, secinp, out] )

The node internal version of hmacSha256 using the crypto module

Params
secret String The secret to generate the hash
val String The value to generate the hash of
[secinp="hex"] String The input encoding of the secret. ( Enum: hex, base64, utf8 )
[out="hex"] String The output encoding of the generated hash. ( Enum: hex, base64, utf8 )
Returns
String The generated hash
API
private
67  
68    _hmacSha256_Crypto: ( secret, val, secinp="hex", out="hex" )=>
69      _hash = @crypto.createHmac('SHA256', new Buffer( secret, secinp ) ).update( val )
70      return _hash.digest( out )
71    
72  
73  

_hmacSha256_CryptoJs

CryptoAdapter._hmacSha256_CryptoJs( secret, val[, secinp, out] )

The browser version of hmacSha256 using the crypto-js module

Params
secret String The secret to generate the hash
val String The value to generate the hash of
[secinp="hex"] String The input encoding of the secret. ( Enum: hex, base64, utf8 )
[out="hex"] String The output encoding of the generated hash. ( Enum: hex, base64, utf8 )
Returns
String The generated hash
API
private
87  
88    _hmacSha256_CryptoJs: ( secret, val, secinp="hex", out="hex" )=>
89      _secinpEnc = @_cryptoJsEnc( secinp )
90      _outEnc = @_cryptoJsEnc( out )
91      _hash = @crypto.HmacSHA256( val, _secinpEnc.parse( secret ) )
92      return _hash.toString(_outEnc )
93    
94  
95  

_cryptoJsEnc

CryptoAdapter._cryptoJsEnc( enc )

crypto-js helper to grab the right encoder

Params
[enc="hex"] String The encoding key. ( Enum: hex, base64, utf8 )
Returns
CryptoJs.Enc A crypto-js encoder
API
private
106  
107    _cryptoJsEnc: ( enc="hex" )=>
108      return switch enc
109        when "base64" then @crypto.enc.Base64
110        when "hex" then @crypto.enc.Hex
111        when "utf8" then @crypto.enc.Utf8
112  
113  
114  

export a instance

113  
114  module.exports = new CryptoAdapter()
115    
116