當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript crypto.pbkdf2Sync函數代碼示例

本文整理匯總了TypeScript中crypto.pbkdf2Sync函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript pbkdf2Sync函數的具體用法?TypeScript pbkdf2Sync怎麽用?TypeScript pbkdf2Sync使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了pbkdf2Sync函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: 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
  })
開發者ID:blockstack,項目名稱:blockstack-cli,代碼行數:31,代碼來源:wallet.ts

示例2: function

UserSchema.methods.hashPassword = function(password) {
    if (this.salt && password) {
        return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString("base64");
    } else {
        return password;
    }
};
開發者ID:cm0s,項目名稱:mea2n,代碼行數:7,代碼來源:user.model.ts

示例3: HashSync

export function HashSync(data: string, salt?: string)
{

    if (typeof salt === 'undefined' || !salt)
    {
        salt = GenSalt();
    }

    const key = crypto.pbkdf2Sync(data, salt, 150000, HASHLENGHT, 'sha512').toString('hex');
    return salt + key;
}
開發者ID:Kasperki,項目名稱:NodeBlog,代碼行數:11,代碼來源:Hash.ts

示例4: makeHash

export function makeHash(decryptedPassword:string, salt?:string):hashResult {
    if (!salt) {
      salt = crypto.randomBytes(16).toString('base64')
    }
    const saltBuf = new Buffer(salt, 'base64')
    const hashedPassword = crypto.pbkdf2Sync(`${LOCAL_AUTH_SALT}${decryptedPassword}`, saltBuf, 10000, 512, 'sha512')
    return {
        hashedPassword: hashedPassword.toString(),
        salt: salt
    }
}
開發者ID:memolog,項目名稱:mean-edge,代碼行數:11,代碼來源:local.ts

示例5: encryptPassword

  private encryptPassword(password: string, salt: string): string{
    if (!password || !salt) {
      return null;
    }
    
    let defaultIterations = 10000;
    let defaultKeyLength = 64;
    let base64Salt = new Buffer(salt, 'base64');

    return crypto.pbkdf2Sync(password, base64Salt, defaultIterations, defaultKeyLength, 'sha512')
                  .toString('base64');
  }
開發者ID:steelheaddigital,項目名稱:angular2-express-starter,代碼行數:12,代碼來源:user_service.ts

示例6: hashPassword

export function hashPassword(plaintext: string, salt: string, options: IPasswordOptions): string {
  assert.ok(salt.length === options.saltLength, 'incompatible salt length')

  return crypto
    .pbkdf2Sync(
      plaintext,
      salt + options.secret,
      options.iterations,
      options.hashedPasswordLength / 2,
      options.algorithm,
    )
    .toString('hex')
}
開發者ID:patrickhulce,項目名稱:klay,代碼行數:13,代碼來源:password.ts

示例7: PasswordError

  return Promise.resolve().then(() => {
    const salt = dataBuffer.slice(0, 16)
    const hmacSig = dataBuffer.slice(16, 48)   // 32 bytes
    const cipherText = dataBuffer.slice(48)
    const hmacPayload = Buffer.concat([salt, cipherText])

    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 decipher = crypto.createDecipheriv('aes-128-cbc', encKey, iv)
    let plaintext = decipher.update(cipherText).toString('hex')
    plaintext += decipher.final().toString('hex')

    const hmac = crypto.createHmac('sha256', macKey)
    hmac.write(hmacPayload)
    const hmacDigest = hmac.digest()

    // hash both hmacSig and hmacDigest so string comparison time
    // is uncorrelated to the ciphertext
    const hmacSigHash = crypto.createHash('sha256')
      .update(hmacSig)
      .digest()
      .toString('hex')

    const hmacDigestHash = crypto.createHash('sha256')
      .update(hmacDigest)
      .digest()
      .toString('hex')

    if (hmacSigHash !== hmacDigestHash) {
      // not authentic
      throw new PasswordError('Wrong password (HMAC mismatch)')
    }

    const mnemonic = bip39.entropyToMnemonic(plaintext)
    if (!bip39.validateMnemonic(mnemonic)) {
      throw new PasswordError('Wrong password (invalid plaintext)')
    }

    return mnemonic
  })
開發者ID:blockstack,項目名稱:blockstack-cli,代碼行數:43,代碼來源:wallet.ts

示例8:

export const grades = functions.pubsub.topic('grades').onPublish(event => {
  const { chicagoId, record } = event.data.json;
  const basis = [record['term'], record['course'], record['section']].join();
  const key = crypto
    .pbkdf2Sync(basis, chicagoId, 2000000, 20, 'sha512')
    .toString('base64')
    .replace(/=/g, '')
    .replace(/\+/g, '-')
    .replace(/\//g, '_');
  return admin
    .firestore()
    .collection('institutions')
    .doc('uchicago')
    .collection('grades')
    .doc(key)
    .set({
      course: record['course'],
      section: record['section'],
      term: record['term'],
      gpa: record['gpa'],
      tenure: record['tenure'],
    });
});
開發者ID:kevmo314,項目名稱:canigraduate.uchicago.edu,代碼行數:23,代碼來源:index.ts

示例9: encryptPassword

			 */
			encryptPassword(password, callback): any {
			  if (!password || !this.salt) {
			    if (!callback) {
			      return null;
			    } else {
			      return callback('Missing password or salt');
			    }
			  }

			  let defaultIterations = 10000;
			  let defaultKeyLength = 64;
			  let salt = new Buffer(this.salt, 'base64');

			  if (!callback) {
			    return crypto.pbkdf2Sync(password, salt, defaultIterations, defaultKeyLength, 'sha512')
			      .toString('base64');
			  }

			  return crypto.pbkdf2(password, salt, defaultIterations, defaultKeyLength, 'sha512', (err, key) => {
			    if (err) {
			      callback(err);
			    } else {
			      callback(null, key.toString('base64'));
			    }
			  });
			}
		}
	});

export default User;
開發者ID:projectSHAI,項目名稱:expressgular2,代碼行數:31,代碼來源:user.model.ts

示例10: function

 encryptPassword: function (password) {
     if (!password || !this.salt) { return ''; }
     var salt = new Buffer(this.salt, 'base64');
     return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
 }
開發者ID:NovaeWorkshop,項目名稱:Nova,代碼行數:5,代碼來源:user.model.ts


注:本文中的crypto.pbkdf2Sync函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。