如何在angular6项目中实现bcrypt

时间:2018-11-24 10:27:45

标签: angular typescript cryptography bcrypt

您好,我正在尝试使用bcrypt npm包对密钥进行哈希处理,但这会引发错误。到目前为止,我已经尝试了这段代码。

import * as bcrypt from 'bcrypt';

export class LowLevelEncryption {

    encrypt(key: string){
        console.log(bcrypt);
         bcrypt.hash(key, 19, function(err, hash) {
           Store hash in your password DB.
         });
    }

}

它会引发许多错误,例如:-

Module not found: Error: Can't resolve 'tls' in '/home/ec2-user/environment/passcript/node_modules/tunnel-agent'

可以请某人帮助我或任何其他图书馆的人。 谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试使用crypto-js,并使用所需的任何策略,例如:

private void calculationGrid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 22 && Control.ModifierKeys == Keys.Control)
            {
                calculationGrid.CurrentCell.Value = Clipboard.GetText().Replace("\r", "").Replace("\n", "").Trim();                      
                calculationGrid.BeginEdit(true);
                calculationGrid.NotifyCurrentCellDirty(true);
                calculationGrid.EndEdit();
            }
        }

或更简单:加密

    import { Injectable } from '@angular/core';
    import * as crypto from 'crypto-js';

    @Injectable()
    export class CryptoService {

    constructor() {
    }


   encrypt(key: any, privateKey: any) {
     const cryptkey = crypto.enc.Utf8.parse(privateKey);
     const encrypted = crypto.AES.encrypt(key, cryptkey, {
      iv: crypto.enc.Hex.parse('00000000000000000000000000000000'),
      mode: crypto.mode.ECB,
      padding: crypto.pad.Pkcs7
   });
   return encrypted.toString();
  }


  decrypt(cryptedKey: any, privateKey: any) {
    const cryptoPrivateKey = crypto.enc.Utf8.parse(privateKey);
    const encryptedKey = crypto.enc.Base64.parse(cryptedKey);
    const decrypted = crypto.AES.decrypt({ciphertext: encryptedKey}, cryptoPrivateKey, {
      iv: crypto.enc.Hex.parse('00000000000000000000000000000000'),
      mode: crypto.mode.ECB,
      padding: crypto.pad.Pkcs7
   });
   return decrypted.toString(crypto.enc.Utf8);
  }
}

并解密

const encryptedPassword = crypto.AES.encrypt(value, 'password');

希望这会有所帮助