Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function _hashPassword(tag, key, length, type) {
let hash = CryptoJS.HmacSHA1(tag, key).toString(CryptoJS.enc.Base64);
hash = hash.slice(0, -1); // remove trailing '=' to ensure compatibility with Twik
let sum = 0;
for (let i = 0; i < hash.length; i++) {
sum += hash.charCodeAt(i);
}
/* Parse password to match the request type */
if (type === PASSWORD_TYPES.NUMERIC) {
hash = convertToDigits(hash, sum, length);
} else {
/* We force digits, punctuation characters and mixed case */
// Force digits
hash = injectCharacter(hash, 0, 4, sum, length, '0', 10);
if (type == PASSWORD_TYPES.SPECIAL) {
// Force special chars
let date:string = new Date().toISOString();
let secret:string = securityToken.secret;
let message = '';
if (body !== null && body !== '' && (method === 'PUT' || method === 'POST' || method === 'PATCH')) {
message = method + body + url + date;
} else {
message = method + url + date;
}
console.log('securityToken', securityToken);
if (securityToken.isEncoding('HmacSHA256')) {
options.headers.set(AppUtils.HEADER_X_DIGEST, CryptoJS.HmacSHA256(message, secret).toString());
} else if (securityToken.isEncoding('HmacSHA1')) {
options.headers.set(AppUtils.HEADER_X_DIGEST, CryptoJS.HmacSHA1(message, secret).toString());
} else if (securityToken.isEncoding('HmacMD5')) {
options.headers.set(AppUtils.HEADER_X_DIGEST, CryptoJS.HmacMD5(message, secret).toString());
}
options.headers.set(AppUtils.HEADER_X_ONCE, date);
console.log('url',url);
console.log('message',message);
console.log('secret',secret);
console.log('hmac message',options.headers.get(AppUtils.HEADER_X_DIGEST));
}
}
setOptions(options?: RequestOptionsArgs):RequestOptionsArgs {
const encodedParameters = percentEncodeParameters(params);
const upperCaseHTTPMethod = httpMethod.toUpperCase();
const encodedRequestURL = encodeURIComponent(requestURL);
const encodedConsumerSecret = encodeURIComponent(consumerSecret);
const signatureBaseString = upperCaseHTTPMethod +
'&' + encodedRequestURL +
'&' + encodeURIComponent(encodedParameters);
let signingKey;
if (tokenSecret !== undefined) {
signingKey = encodedRequestURL + '&' + encodeURIComponent(tokenSecret);
} else {
signingKey = encodedConsumerSecret + '&';
}
const signature = Crypto.HmacSHA1(signatureBaseString, signingKey);
const encodedSignature = Crypto.enc.Base64.stringify(signature);
return encodedSignature;
}
function hmacSign(type, secret, content) {
return cryptojs.HmacSHA1(content,secret).toString();
}
exports.hmacSha1 = function(encodedFlags, secretKey) {
var encoded = CryptoJS.HmacSHA1(encodedFlags, secretKey).toString(CryptoJS.enc.Base64);;
return encoded;
}
function getV2SignatureKey(key, stringToSign) {
var words = CryptoJS.HmacSHA1(stringToSign, key);
return CryptoJS.enc.Base64.stringify(words);
}
function getSignature(baseString) {
b64pad = '=';
var signature = CryptoJS.HmacSHA1(baseString, this.key).toString(CryptoJS.enc.Base64);
return signature;
}
));