2017-03-01 22 views
0

有一個函數crypto.pbkdf2Sync() in node.js API,我想在我的Angular2項目中使用它。Angular2:如何在node.js前端使用「crypto.pbkdf2Sync」功能

我試圖導入並使用它。項目編譯沒有錯誤,但在瀏覽器中我得到一個錯誤:

TypeError: webpack_require.i(...) is not a function at createHashSlow (hash.ts:4)

這裏是hash.ts模塊:

import { pbkdf2Sync } from 'crypto'; 
import { CONFIG } from '../config'; 

export function createHashSlow(password, salt) { 
    return pbkdf2Sync(
    password, 
    salt, 
    CONFIG.crypto.hash.iterations, 
    CONFIG.crypto.hash.length, 
    'SHA1' 
).toString('base64'); 
}; 

我做了什麼錯?如何使它工作?

回答

0

crypto模塊是基於OpenSSL的,是不是在瀏覽器中

The crypto module provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign and verify functions.

我建議使用WebCryptographyApi是在所有現代瀏覽器可用可用。在此處看到一個示例Angular JS Cryptography. pbkdf2 and iteration

+0

非常感謝! –