2013年4月2日 星期二

NodeJS ssh sign and verify : ssh-signer


Ssh-signer(GitHub: https://github.com/peihsinsu/ssh-signer),開發的目的是希望提供一個使用crypto與http-signature的簽章與驗證工具...讓client端與server端可以直接安裝一個套件就可以做到簽章跟認證的動作...

安裝

npm install ssh-signer

使用

準備...載入相關modules並且準備opt參數(選用,不一定要放,沒放會使用預設 alg=RSA-SHA256, hash=base64)
var signer = require('ssh-signer')
  , fs = require('fs');

var opt = {
  alg:'RSA-SHA256',
  hash:'base64'
}
SSH key簽章 (opt選用)
//sign a key from public key path
var a = signer.signPrivateKey( 'Test123', '/root/.ssh/id_rsa', opt);

//sign a key from public key string
var privKeyStr = fs.readFileSync('/root/.ssh/id_rsa', 'UTF-8');
var a = signer.signPrivateKeyStr( 'Test123', privKeyStr, opt);
驗證 (opt選用)
//verify a key from public key path
var b = signer.verify(a, 'Test123', '/root/.ssh/id_rsa.pub', opt);

//verify a key from public key string
var pubKeyStr = fs.readFileSync('/root/.ssh/id_rsa.pub', 'UTF-8');
var b = signer.verifyStr(a, 'Test123', pubKeyStr, opt );
console.log('Verify result ==> ' + b); //will show true or false