I can make an HMAC using the following:
var encrypt = crypto.createHmac(\"SHA256\", secret).update(string).digest(\'base64\');
I am trying
As already been stated by CodesInChaos, HMAC with SHA256 can only be used to hash a value, which is a one-way trip only. If you want to be able to encrypt/decrypt you will have to use a cipher, such as aes or des.
Example on how encryption/decryption:
const crypto = require("crypto");
// key and iv
var key = crypto.createHash("sha256").update("OMGCAT!", "ascii").digest();
var iv = "1234567890123456";
// this is the string we want to encrypt/decrypt
var secret = "ermagherd";
console.log("Initial: %s", secret);
// create a aes256 cipher based on our password
var cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
// update the cipher with our secret string
cipher.update(secret, "ascii");
// save the encryption as base64-encoded
var encrypted = cipher.final("base64");
console.log("Encrypted: %s", encrypted);
// create a aes267 decipher based on our password
var decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
// update the decipher with our encrypted string
decipher.update(encrypted, "base64");
console.log("Decrypted: %s", decipher.final("ascii"));
Note: You have to save the cipher/decipher into their own variable, and also make sure not to chain .final after .update.
If you want to know what ciphers are available on your system, use the following command:
openssl list-cipher-algorithm