How to do Base64 encoding in node.js?

前端 未结 7 1336
渐次进展
渐次进展 2020-11-22 16:46

Does node.js have built-in base64 encoding yet?

The reason why I ask this is that final() from crypto can only output hex, binary or ascii

7条回答
  •  情书的邮戳
    2020-11-22 17:24

    crypto now supports base64 (reference):

    cipher.final('base64') 
    

    So you could simply do:

    var cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key, iv);
    var ciph = cipher.update(plaintext, 'utf8', 'base64');
    ciph += cipher.final('base64');
    
    var decipher = crypto.createDecipheriv('des-ede3-cbc', encryption_key, iv);
    var txt = decipher.update(ciph, 'base64', 'utf8');
    txt += decipher.final('utf8');
    

提交回复
热议问题