My plan is to convert a passphrase in UTF-8 ASCII-US to Unit8Array(32) then use it in:

256bitsKey = Unit8Array(32) from passphase

iv = window.crypto.getRandomValues(new Uint8Array(32));

CryptoJS.enc.encrypt(data, 256bitsKey , {iv: iv});

Is there any way to do it?

  • 3
    "Key Derivation Functions" is the term you are looking for. Look up PBKDF2
    – nobody
    Aug 2, 2021 at 8:57
  • 2
    Argon2id is the state of the art for key derivation from a password. PBKDF2 is an old standard.
    – A. Hersean
    Aug 2, 2021 at 9:16
  • @A.Hersean Yeah, right. I couldn't remember the name of Argon2 and I couldn't be bothered to look it up, so I just put in PBKDF. I guess that's a bad thing to do when giving security advice though...
    – nobody
    Aug 2, 2021 at 9:22
  • @nobody very bad... you should have gone with key derivation and let the OP search current standard
    – nethero
    Aug 2, 2021 at 9:57
  • I guess the huge advantage of PBKDF2 is its name. Because when you look at the algorithm, it's not that good.
    – A. Hersean
    Aug 2, 2021 at 11:29

1 Answer 1


You can use the Web Crypto API's window.crypto.subtle.deriveBits() function to derive a key from a password, and return a Unit8Array containing the derived key. Here is an example:

var password='p4$$w0rd';
var pbkdf2iterations=10000;
var passphrasebytes=new TextEncoder("utf-8").encode(password);
var pbkdf2salt=window.crypto.getRandomValues(new Uint8Array(8));

window.crypto.subtle.importKey('raw', passphrasebytes, {name: 'PBKDF2'}, false, ['deriveBits'])
.then(function(result) {
    var passphrasekey=result;
    return pbkdf2bytes=window.crypto.subtle.deriveBits({"name": 'PBKDF2', "salt": pbkdf2salt, "iterations": pbkdf2iterations, "hash": 'SHA-256'}, passphrasekey, 256)       
}).then(function(result) {  
    pbkdf2bytes=new Uint8Array(result);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .