How can I decrypt a HMAC?

前端 未结 4 513
名媛妹妹
名媛妹妹 2020-12-29 09:29

I can make an HMAC using the following:

var encrypt = crypto.createHmac(\"SHA256\", secret).update(string).digest(\'base64\');

I am trying

4条回答
  •  长情又很酷
    2020-12-29 10:05

    Clean-up of code for a Minimalist View and removal of clutter: note: IIFE runnable in node repl "As Is"

    !function(){
    
    const crypto = require("crypto");
    
    // key 
    var key = crypto.createHash("sha256").digest();
    
    
     // 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.createCipher("aes-256-cbc", key);
    
    // update the cipher with our secret string
    cipher.update(secret);
    
    // save the encryption 
    var encrypted = cipher.final();
    
    console.log("Encrypted: %s", encrypted);
    
    // create a aes267 decipher based on our password
     var decipher = crypto.createDecipher("aes-256-cbc", key);
    
    // update the decipher with our encrypted string
    decipher.update(encrypted);
    
    console.log("Decrypted: %s", decipher.final()); //default is utf8 encoding   final("utf8") not needed for default
    
    }()
    
    /*  REPL Output
    
                Initial: ermagherd
        Encrypted: T)��l��Ʀ��,�'
        Decrypted: ermagherd
        true
    */
    

提交回复
热议问题