File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Integration/Scripted REST Api/Webhook receiver with HMAC SHA-256 validation Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ // Script Include: HmacUtils
2+ // Purpose: Compute HMAC SHA-256 and constant-time compare.
3+
4+ var HmacUtils = Class . create ( ) ;
5+ HmacUtils . prototype = {
6+ initialize : function ( ) { } ,
7+
8+ hmacSha256Hex : function ( secret , message ) {
9+ var mac = Packages . javax . crypto . Mac . getInstance ( 'HmacSHA256' ) ;
10+ var key = new Packages . javax . crypto . spec . SecretKeySpec (
11+ new Packages . java . lang . String ( secret ) . getBytes ( 'UTF-8' ) ,
12+ 'HmacSHA256'
13+ ) ;
14+ mac . init ( key ) ;
15+ var raw = mac . doFinal ( new Packages . java . lang . String ( message ) . getBytes ( 'UTF-8' ) ) ;
16+
17+ var sb = new Packages . java . lang . StringBuilder ( ) ;
18+ for ( var i = 0 ; i < raw . length ; i ++ ) {
19+ var hex = Packages . java . lang . Integer . toHexString ( ( raw [ i ] & 0xff ) | 0x100 ) . substring ( 1 ) ;
20+ sb . append ( hex ) ;
21+ }
22+ return sb . toString ( ) ;
23+ } ,
24+
25+ constantTimeEquals : function ( a , b ) {
26+ var A = String ( a || '' ) ;
27+ var B = String ( b || '' ) ;
28+ if ( A . length !== B . length ) return false ;
29+ var diff = 0 ;
30+ for ( var i = 0 ; i < A . length ; i ++ ) diff |= A . charCodeAt ( i ) ^ B . charCodeAt ( i ) ;
31+ return diff === 0 ;
32+ } ,
33+
34+ type : 'HmacUtils'
35+ } ;
You can’t perform that action at this time.
0 commit comments