0

I know its not possible to translate a programming language to another one, but now i'm trying to encrypte something using nodejs, and in the docs that im working with they dont have an example for nodejs they have only for PHP,

So my question how can I write this code

base64encode(hex(sha256("shared_secret")))

in Nodejs

Doe
  • 1
  • 1
  • 1
    Possible duplicate of [NodeJS - SHA256 Password Encryption](http://stackoverflow.com/questions/19236327/nodejs-sha256-password-encryption) – Ben Fortune Sep 23 '16 at 10:30
  • @BenFortune I already tried this answer but didnt not work for me – Doe Sep 23 '16 at 10:31

2 Answers2

0

Maybe that can help you, but if you want to encrypt password this will be more helpful I think.

var crypto = require('crypto');
var hash = crypto.createHash('sha256').update(pwd).digest('hex').digest('base64');
Community
  • 1
  • 1
0

If you’re writing code that doesn’t need to support IE9 or earlier, then you can use btoa() and atob() to convert to and from base64 encoding. Otherwise, use something like the function Sunny referenced.

There appears to be some confusion in the comments regarding what these functions accept/return, so…

btoa() accepts a “string” where each character represents an 8-bit byte – if you pass a string containing characters that can’t be represented in 8 bits, it will probably break. This isn’t a problem if you’re actually treating the string as a byte array, but if you’re trying to do something else then you’ll have to encode it first. atob() returns a “string” where each character represents an 8-bit byte – that is, its value will be between 0 and 0xff. This does not mean it’s ASCII – presumably if you’re using this function at all, you expect to be working with binary data and not text.

function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    }));
}

b64EncodeUnicode('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
b64EncodeUnicode('\n'); // "Cg=="

https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

Shiju Augustine
  • 265
  • 1
  • 4
  • 11