本文整理匯總了TypeScript中crypto.createCipheriv函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript createCipheriv函數的具體用法?TypeScript createCipheriv怎麽用?TypeScript createCipheriv使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了createCipheriv函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: encryptWithIV
function encryptWithIV(text: string, iv: Buffer, opts: Options): string {
setupKeys(opts);
const cipher = createCipheriv(
opts.encryptionAlgorithm as string,
opts.encryptionKey,
iv
);
const plaintext = Buffer.from(text, 'utf8');
const ciphertextStart = forceBuffer(cipher.update(plaintext));
zeroBuffer(plaintext);
const ciphertextEnd = forceBuffer(cipher.final());
const ciphertext = Buffer.concat([ciphertextStart, ciphertextEnd]);
zeroBuffer(ciphertextStart);
zeroBuffer(ciphertextEnd);
const mac = computeHmac(iv, ciphertext, opts);
const result = [
B.encode(iv),
B.encode(ciphertext),
B.encode(mac),
].join('.');
zeroBuffer(iv);
zeroBuffer(ciphertext);
zeroBuffer(mac);
return result;
}
示例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: encryptPrivateKey
export function encryptPrivateKey(privKey, pubKey, encryptionKey) {
const key = encryptionKey;
const doubleHash = Buffer.from(SHA256(SHA256(pubKey)), 'hex');
const iv = doubleHash.slice(0, 16);
const cipher = crypto.createCipheriv(algo, key, iv);
const encData = cipher.update(privKey, 'utf8', 'hex') + cipher.final('hex');
return encData;
}
示例4: encryptEncryptionKey
export function encryptEncryptionKey(encryptionKey, password) {
const password_hash = Buffer.from(SHA512(password));
const key = password_hash.slice(0, 32);
const iv = password_hash.slice(32, 48);
const cipher = crypto.createCipheriv(algo, key, iv);
const encData =
cipher.update(encryptionKey, 'hex', 'hex') + cipher.final('hex');
return encData;
}
示例5:
export const encryptValue = (value: string): string => {
const iv = IV_LENGTH ? crypto.randomBytes(IV_LENGTH) : null;
const cipher = crypto.createCipheriv(ENCRYPTION_TYPE, Buffer.from(ENCRYPTION_KEY), iv);
const encrypted = cipher.update(value);
return [
ENCRYPTION_TYPE,
iv.toString('hex'),
Buffer.concat([encrypted, cipher.final()]).toString('hex')
].join(':');
};
示例6: encrypt
export function encrypt(mediaUrl: string, decryptKey: string, iv?: string) {
const ivHex = iv ? Buffer.from(iv, 'hex') : randomBytes(IV_LENGTH)
const encipher = createCipheriv(ALGORITHM, decryptKey, ivHex)
const encrypted = Buffer.concat([
ivHex,
encipher.update(mediaUrl),
encipher.final()
])
return encrypted.toString('base64')
}
示例7: pbkdf2
return pbkdf2(password, salt, pbkdf2Iterations, pbkdf2KeyLen, pbkdf2Digest).then((key) => {
const cipher = crypto.createCipheriv(cipherAlgorithm, key, iv);
let value = cipher.update(JSON.stringify(json), 'utf8', 'base64');
value += cipher.final('base64');
let data: EncryptedJson<T> = {
salt: salt.toString('base64'),
iv: iv.toString('base64'),
value: value
};
let tag = cipher.getAuthTag();
if (tag) {
data.tag = tag.toString('base64');
}
return data;
});
示例8: encryptAES
export function encryptAES(params: any, config: IOPayConfig) {
if (typeof params != "object")
throw new Error(`Error encryptAES: params is invalid.`);
let sec = ["HashKey", "HashIV"];
sec.forEach(key => {
delete params[key];
});
let encipher = crypto.createCipheriv(
"aes-128-cbc",
config.AIOHashKey,
config.AIOHashIV
);
let text = params["PaymentToken"];
let encrypted_base64 = Buffer.concat([
encipher.update(text),
encipher.final()
]).toString("base64");
return urlEncodeDotNet(encrypted_base64);
}
示例9: encrypt
/**
* Encrypts the object by JSON serializing it and then calling AES.
*/
public encrypt(data: string): string {
Logger.log.debug("AesEncryptor.encrypt: start.");
if (!this.passphrase) {
throw new EncryptionError("Encryption passphrase not available.", "AES256");
}
let params: EncryptionParameters = this.createKeyAndIV();
let cipher: Crypto.Cipher = Crypto.createCipheriv("aes256", params.key, params.iv);
let encrypted: string = cipher.update(data, "utf8", "base64");
encrypted += cipher.final("base64");
params.data = Buffer.concat([new Buffer("Salted__", "utf8"), params.salt, new Buffer(encrypted, "base64")]);
Logger.log.debug(`AesEncryptor.encrypt: encryption complete: '${encrypted}'.`);
return params.data.toString("base64");
}
示例10: aes_encode
static aes_encode(data: string): string {
//使用的加密算法
var algorithm ='AES-128-ECB';
//使用的加密字符串
var key = 'jydasai38hao1616';
//輸入的數據編碼
var inputEncoding = 'utf8';
//初始化向量
//輸出數據編碼
var outputEncoding = 'base64';
//創建加密器
var cipher = crypto.createCipheriv(algorithm, key,"");
cipher.setAutoPadding(true);
//更新加密器:對數據進行加密
var encodingStr = cipher.update(data,inputEncoding,outputEncoding);
encodingStr += cipher.final(outputEncoding);
//返回加密後字符串
return encodingStr;
}