fs.createReadStream(file) .pipe(zlib.createGzip()) .pipe(crypto.createCipher('aes-192-gcm', 'secret')) .pipe(req) .on('finish', () => { console.log('File successfully sent') })
_.extend({ getKey: function(password) { let keyGenConfig = config.keyGen return crypto.pbkdf2Sync(password, keyGenConfig.salt, keyGenConfig.iterations, keyGenConfig.length, keyGenConfig.digest) }, getPasswordHash: function(password){ let key = this.getKey(password) hash = crypto.createHash('sha256') return hash.update(key).digest('base64') }, encrypt: function(payload, iv, key = config.key, algorithm = config.cipherAlgo){ key = key.slice(0, config.aes.keySize) let cipher = crypto.createCipheriv(algorithm, key, iv) return cipher.update(payload, 'utf8', 'base64') + cipher.final('base64') }, decrypt: function(payload, iv, key = config.key, algorithm = config.cipherAlgo){ key = key.slice(0, config.aes.keySize) let decipher = crypto.createDecipheriv(algorithm, key, iv) return decipher.update(payload, 'base64', 'utf8') + decipher.final('utf8') }, verifyPassword: function(passoword, passwordHash){ return this.getPasswordHash(passoword) == passwordHash } }, crypto)
const encrypted = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]); const tag = cipher.getAuthTag();
// 用于缓存tokenHash,防止高峰期数据库爆炸💥 // 数据库迁移代码 // (async() => { // console.log('正在迁移auth数据库') // let allUsers = await db.auth.find({}, -1) // let authCollection = await mongodb('herald_auth') // if (allUsers.length > 0) { // await authCollection.insertMany(allUsers) // } // console.log(`共迁移${allUsers.length}条记录`) // })(); // 对称加密算法,要求 value 是 String 或 Buffer,否则会报错 const encrypt = (key, value) => { try { let cipher = crypto.createCipher(config.auth.cipher, key) let result = cipher.update(value, 'utf8', 'hex') result += cipher.final('hex') return result } catch (e) { return '' } }
const encrypted = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]); const tag = cipher.getAuthTag();
const encrypted = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]); const tag = cipher.getAuthTag();
const encrypted = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]); const tag = cipher.getAuthTag();