本文整理汇总了TypeScript中crypto.createHmac函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createHmac函数的具体用法?TypeScript createHmac怎么用?TypeScript createHmac使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createHmac函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: reject
worker.request = new Promise<PhantomResult>((resolve, reject) => {
const hmac = crypto.createHmac("sha256", worker.key)
.update([worker.counter, 'POST', '/', `localhost:${worker.port}`, 'application/json', payload].join('\n'))
.digest('hex');
http.request({
port: worker.port,
method: 'POST',
auth: worker.counter + ':' + hmac,
headers: {
'Content-Type': 'application/json',
'Content-Length': payload.length,
},
}, (response) => {
let result = '';
response.on('error', (error) => {
reject(error);
});
response.on('data', (data) => {
result += data;
});
response.on('end', () => {
resolve([response, result]);
});
})
.on('error', (error) => {
reject(error);
})
.end(payload);
});
示例2: Error
return Promise.resolve().then(() => {
// must be bip39 mnemonic
if (!bip39.validateMnemonic(phrase)) {
throw new Error('Not a valid bip39 nmemonic')
}
// normalize plaintext to fixed length byte string
const plaintextNormalized = Buffer.from(
bip39.mnemonicToEntropy(phrase), 'hex'
)
// AES-128-CBC with SHA256 HMAC
const salt = crypto.randomBytes(16)
const keysAndIV = crypto.pbkdf2Sync(password, salt, 100000, 48, 'sha512')
const encKey = keysAndIV.slice(0, 16)
const macKey = keysAndIV.slice(16, 32)
const iv = keysAndIV.slice(32, 48)
const cipher = crypto.createCipheriv('aes-128-cbc', encKey, iv)
let cipherText = cipher.update(plaintextNormalized).toString('hex')
cipherText += cipher.final().toString('hex')
const hmacPayload = Buffer.concat([salt, Buffer.from(cipherText, 'hex')])
const hmac = crypto.createHmac('sha256', macKey)
hmac.write(hmacPayload)
const hmacDigest = hmac.digest()
const payload = Buffer.concat([salt, hmacDigest, Buffer.from(cipherText, 'hex')])
return payload
})
示例3: function
prototype.request = function(method, uriParts, opts, callback) {
var self = this;
opts = opts || {};
method = method.toUpperCase();
if (!callback && (opts instanceof Function)) {
callback = opts;
opts = {};
}
var relativeURI = self.makeRelativeURI(uriParts);
_.assign(opts, {
'method': method,
'uri': self.makeAbsoluteURI(relativeURI),
});
if (opts.body && (typeof opts.body !== 'string')) {
opts.body = JSON.stringify(opts.body);
}
opts.agent = keepaliveAgent;
var timestamp = Date.now() / 1000;
var what = timestamp + method + relativeURI + (opts.body || '');
var key = new Buffer(self.b64secret, 'base64');
var hmac = crypto.createHmac('sha256', key);
var signature = hmac.update(what).digest('base64');
self.addHeaders(opts, {
'CB-ACCESS-KEY': self.key,
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-PASSPHRASE': self.passphrase,
});
request(opts, self.makeRequestCallback(callback));
};
示例4: TypeError
export function unsignSync<T extends UnknownRecord>(
signature: string,
secret: string,
options?: SignaturOptions
): T {
const { separator = '.' } = options || {} as SignaturOptions;
if ('string' !== typeof(signature) || !signature.length) {
throw new TypeError(`Expected 'signature' to be defined, but received '${signature}'`);
}
if ('string' !== typeof(secret) || !secret.length) {
throw new TypeError(`Expected 'secret' to be defined, but received '${secret}'`);
}
const [hash, enc] = signature.split(separator, 2);
const decoded = Buffer.from(
(hash + '==='.slice((hash.length + 3) % 4))
.replace(/\-/gi, '+')
.replace(/_/gi, '/'), 'base64')
.toString('utf8');
const signedDecoded = urlSafeBase64(
createHmac('sha256', secret).update(decoded).digest('base64'));
if (enc !== signedDecoded) {
throw new SignaturError('invalid_signature', 'Signature not match');
}
return (JSON.parse(decoded) as DecodedData<T>).data;
}
示例5: makeNewUser
private makeNewUser(email: string, password: string) {
const solt = crypto.randomBytes(16).toString('hex');
const hashPassword = crypto
.createHmac("sha256", solt)
.update(password)
.digest('hex');
const newUser = {
UTC: new Date(),
email: email,
solt: solt,
password: hashPassword,
};
this.sendMailToNewUser(email);
return new Promise((resolve) => {
this.db.collection("users").insert(newUser, (err) => {
if (err) {
resolve(false);
return;
}
resolve(true);
return;
});
});
}
示例6: computeHmac
function computeHmac(iv: Buffer, ciphertext: Buffer, opts: Options) {
const hmac = createHmac(opts.signatureAlgorithm as string, opts.signatureKey as Buffer);
hmac.update(iv);
hmac.update(ciphertext);
return forceBuffer(hmac.digest());
}
示例7: checkPassword
/**
* @returns {bool}
*/
checkPassword (password: string) {
const hashed = crypto
.createHmac('sha1', this.salt + config.app.secret)
.update(password)
.digest('hex');
return hashed === this.password;
}
示例8: return
const signBlob = (exports.signBlob = (key: string, blob: string) => {
return (
"sha1=" +
crypto
.createHmac("sha1", key)
.update(blob)
.digest("hex")
);
});
示例9: function
userSchema.methods.hashPassword = function(password: string): string {
if (!password) {
return '';
}
if (!this.password_salt) {
return '';
}
return createHmac('sha1', this.password_salt).update(password).digest('hex');
};
示例10: webhookAuth
export function webhookAuth(req, res, next) {
const createdHmac = createHmac("SHA256", getEnv(SHOPIFY_SHARED_SECRET)).update(new Buffer(req.rawBody, 'utf8')).digest('base64');
const hmacHeader = req.get('HTTP_X_SHOPIFY_HMAC_SHA256');
if (createdHmac !== hmacHeader) {
console.warn('HTTP_X_SHOPIFY_HMAC_SHA256 header does not match calculated HMAC');
return res.sendStatus(401);
}
next()
}