V
V
Vladimir Kuts2016-09-04 19:55:57
Python
Vladimir Kuts, 2016-09-04 19:55:57

R.S.A. Similar to nodejs?

In python, I get a hash with this series of commands:

>>> import rsa
>>> import base64
>>> from django.conf import settings
>>> with open(settings.KEY_FILE, 'rb') as keyf:
...     priv_key = rsa.PrivateKey.load_pkcs1(keyf.read(), format='PEM')
...
>>> mess = 'var1=123\r\nvar=456\r\n'

>>> sign = rsa.sign(mess, priv_key, "SHA-1")
>>> import urllib

>>> from base64 import standard_b64encode
>>> b64sign = urllib.quote(standard_b64encode(sign), safe='')
>>> b64sign

The question is what is the analogue in Node.js. All the options I tried do not return the same hash that python returns
. In node.js, I use commands like this:
const NodeRSA = require('node-rsa')
const urlencode = require('urlencode')

fs.readFile(config.keyFile, 'ascii', function (err,content) {   // keyFile тот же что и в Python
        if (err) {
            return console.log('error ', err);
        }
        rsa_key = content

       var my_hash = mess  // mess такой же как в Python
        var key = new NodeRSA(rsa_key, 'pkcs1-private-pem')

        key.setOptions({signingScheme: 'pss-sha1'})  // тут перебирал все доступные варианты

        my_hash = key.encryptPrivate(my_hash, 'base64')
        console.log(urlencode(my_hash))
}

hashes and do not closely match

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2016-09-05
@fox_12

I solved the problem with this piece of code:

var out_ = windows1251.encode(out, {})
    var signer = crypto.createSign('RSA-SHA1')
    signer.update(out_)
    var pem = fs.readFileSync(config.keyFile)
    var rsa_key = pem.toString('ascii')
    var res = signer.sign(rsa_key, 'base64')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question