Skip to content

Instantly share code, notes, and snippets.

@MatthieuLemoine
Created January 23, 2017 14:36

Revisions

  1. MatthieuLemoine created this gist Jan 23, 2017.
    19 changes: 19 additions & 0 deletions crypto.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    const id = '__JUNK__';
    // Public key need to be in PKCS8 format
    // ssh-keygen -e -m PKCS8 -f id_rsa.pub > id_rsa.pkcs8
    const publicKey = fs.readFileSync(path.join(__dirname, 'id_rsa.pkcs8'), { encoding : 'utf8' });
    const privateKey = fs.readFileSync(path.join(__dirname, 'id_rsa'), { encoding : 'utf8' });

    // Sign
    const signer = crypto.createSign('RSA-SHA512');
    signer.update(id);
    const signature = signer.sign(privateKey, 'hex');

    // ...

    // Verify
    const verifier = crypto.createVerify('RSA-SHA512');
    verifier.update(ruid);
    const publicKeyBuf = new Buffer(publicKey, 'utf-8');
    const signatureBuf = new Buffer(signature, 'hex');
    const result = verifier.verify(publicKeyBuf, signatureBuf);