@@ -99,7 +99,9 @@ var _require2 = require('./consts'),
9999 LAYER_KEYWORD_PARAMS = _require2 . LAYER_KEYWORD_PARAMS ,
100100 TRANSFORMATION_PARAMS = _require2 . TRANSFORMATION_PARAMS ,
101101 SIMPLE_PARAMS = _require2 . SIMPLE_PARAMS ,
102- UPLOAD_PREFIX = _require2 . UPLOAD_PREFIX ;
102+ UPLOAD_PREFIX = _require2 . UPLOAD_PREFIX ,
103+ SUPPORTED_SIGNATURE_ALGORITHMS = _require2 . SUPPORTED_SIGNATURE_ALGORITHMS ,
104+ DEFAULT_SIGNATURE_ALGORITHM = _require2 . DEFAULT_SIGNATURE_ALGORITHM ;
103105
104106function textStyle ( layer ) {
105107 var keywords = [ ] ;
@@ -757,6 +759,10 @@ function url(public_id) {
757759 var api_secret = consumeOption ( options , "api_secret" , config ( ) . api_secret ) ;
758760 var url_suffix = consumeOption ( options , "url_suffix" ) ;
759761 var use_root_path = consumeOption ( options , "use_root_path" , config ( ) . use_root_path ) ;
762+ var signature_algorithm = consumeOption ( options , "signature_algorithm" , config ( ) . signature_algorithm || DEFAULT_SIGNATURE_ALGORITHM ) ;
763+ if ( long_url_signature ) {
764+ signature_algorithm = 'sha256' ;
765+ }
760766 var auth_token = consumeOption ( options , "auth_token" ) ;
761767 if ( auth_token !== false ) {
762768 auth_token = exports . merge ( config ( ) . auth_token , auth_token ) ;
@@ -812,9 +818,8 @@ function url(public_id) {
812818 }
813819 // eslint-disable-next-line no-empty
814820 } catch ( error ) { }
815- var shasum = crypto . createHash ( long_url_signature ? 'sha256' : 'sha1' ) ;
816- shasum . update ( utf8_encode ( to_sign + api_secret ) , 'binary' ) ;
817- signature = shasum . digest ( 'base64' ) . replace ( / \/ / g, '_' ) . replace ( / \+ / g, '-' ) . substring ( 0 , long_url_signature ? 32 : 8 ) ;
821+ var hash = computeHash ( to_sign + api_secret , signature_algorithm , 'base64' ) ;
822+ signature = hash . replace ( / \/ / g, '_' ) . replace ( / \+ / g, '-' ) . substring ( 0 , long_url_signature ? 32 : 8 ) ;
818823 signature = `s--${ signature } --` ;
819824 }
820825 var prefix = unsigned_url_prefix ( public_id , cloud_name , private_cdn , cdn_subdomain , secure_cdn_subdomain , cname , secure , secure_distribution ) ;
@@ -1009,9 +1014,24 @@ function api_sign_request(params_to_sign, api_secret) {
10091014
10101015 return `${ k } =${ toArray ( v ) . join ( "," ) } ` ;
10111016 } ) . sort ( ) . join ( "&" ) ;
1012- var shasum = crypto . createHash ( 'sha1' ) ;
1013- shasum . update ( utf8_encode ( to_sign + api_secret ) , 'binary' ) ;
1014- return shasum . digest ( 'hex' ) ;
1017+ return computeHash ( to_sign + api_secret , config ( ) . signature_algorithm || DEFAULT_SIGNATURE_ALGORITHM , 'hex' ) ;
1018+ }
1019+
1020+ /**
1021+ * Computes hash from input string using specified algorithm.
1022+ * @private
1023+ * @param {string } input string which to compute hash from
1024+ * @param {string } signature_algorithm algorithm to use for computing hash
1025+ * @param {string } encoding type of encoding
1026+ * @return {string } computed hash value
1027+ */
1028+ function computeHash ( input , signature_algorithm , encoding ) {
1029+ if ( ! SUPPORTED_SIGNATURE_ALGORITHMS . includes ( signature_algorithm ) ) {
1030+ throw new Error ( `Signature algorithm ${ signature_algorithm } is not supported. Supported algorithms: ${ SUPPORTED_SIGNATURE_ALGORITHMS . join ( ', ' ) } ` ) ;
1031+ }
1032+ var hash = crypto . createHash ( signature_algorithm ) ;
1033+ hash . update ( utf8_encode ( input ) , 'binary' ) ;
1034+ return hash . digest ( encoding ) ;
10151035}
10161036
10171037function clear_blank ( hash ) {
@@ -1053,9 +1073,8 @@ function webhook_signature(data, timestamp) {
10531073 ensurePresenceOf ( { data, timestamp } ) ;
10541074
10551075 var api_secret = ensureOption ( options , 'api_secret' ) ;
1056- var shasum = crypto . createHash ( 'sha1' ) ;
1057- shasum . update ( data + timestamp + api_secret , 'binary' ) ;
1058- return shasum . digest ( 'hex' ) ;
1076+ var signature_algorithm = ensureOption ( options , 'signature_algorithm' , DEFAULT_SIGNATURE_ALGORITHM ) ;
1077+ return computeHash ( data + timestamp + api_secret , signature_algorithm , 'hex' ) ;
10591078}
10601079
10611080/**
@@ -1075,7 +1094,10 @@ function verifyNotificationSignature(body, timestamp, signature) {
10751094 if ( timestamp < Date . now ( ) - valid_for ) {
10761095 return false ;
10771096 }
1078- var payload_hash = utils . webhook_signature ( body , timestamp , { api_secret : config ( ) . api_secret } ) ;
1097+ var payload_hash = utils . webhook_signature ( body , timestamp , {
1098+ api_secret : config ( ) . api_secret ,
1099+ signature_algorithm : config ( ) . signature_algorithm
1100+ } ) ;
10791101 return signature === payload_hash ;
10801102}
10811103
0 commit comments