Friday, 28 April 2017

crypto verifier.verify() always returns false - javascript

I am trying to use the cypto verifier.verify to verify an RSA signature with my public key. When I do it on openssl, the verification returns as verified OK, but when I do it in my program, verifier.verify always returns false.

event is the signature that is passed in

const crypto = require('crypto');
var fs = require('fs');
var expired = false;


const pub = '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApnVi9xDUe6+Kimb2TniZ\nX/pBGIhy5glXpHYgGmTOeYQa45bDkdwn3ydk15M/K7xpwJsHWSM2K2cQzCO7YCu8\nrgEMHggezt69KtXt78ohNy7AwD2Vheo6uXfaR91X9bc2dZ9oR2pEWHh3MX5jXA9y\nRopDfA/xdWj8ZiZK0RPGzyGufRauhFUaf48+2emr0nwluAfl5v1fV7vm/sBxitbj\nwl7rmka6SYodhMPRnMphFbv66sN50fPcZop8kWHp+LcvXB0TIhccRvnPHkTqjc7/\nbGiGQly6cL7JkQrDbcDCuGqPc397qDHb2ABpyZKWCHpkVAwNyc5Y1uOjaSJgPwq6\n8QIDAQAB\n-----END PUBLIC KEY-----\n';

exports.handler = (event, context, callback) => {

// from site https://github.com/nodejs/node-v0.x-archive/issues/6938
//data is signed string that's passed into this in an above not shown function wrapper.
    try{
    //var message = new Buffer(event);   
    var message = event //needs to be just string apparently
    var verifier = crypto.createVerify("RSA-SHA256");
    console.log("message " + message);   
    //verifier.update takes in the string that was signed.
    verifier.update(message); //was data

    //verifier.verify
    //pub is a string of public signature in PEM format
    //signature is the base64 binary encoded version of data
    //base64 tells the format so it can be verified
    //expired is a bool, for this example, hard coded above to false, you'll want to check that for real.

    //console.log('vf: ', verifier.verify(pub, message, 'base64'));


    callback(null, {
        verified: (verifier.verify(pub, message, 'base64'))
    });
    } catch (e) {
    callback("Bad Request: " + e);
    }
};



via Sharon Soleman

No comments:

Post a Comment