上傳一直是寫Web程式的開發人員不可或缺的一個口袋工具
Node.js針對上傳部分,比較普遍的是使用ExpressJS的上傳設定
而原生的http模組,也一樣可以做到上傳...
下面是網路上找到的兩筆不同上傳的範例:
npm install ssh-signer
var signer = require('ssh-signer')
, fs = require('fs');
var opt = {
alg:'RSA-SHA256',
hash:'base64'
}
//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);
//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
var crypto=require("crypto");var sys=require("sys");var fs=require("fs");var hs = require('http-signature');
//讀取public key與private keyvar ENCODE= 'UTF-8';var alg = "RSA-SHA256";var n = new Date().toUTCString();
//public keyvar pubKey = fs.readFileSync("/root/.ssh/id_rsa.pub", ENCODE);//private keyvar privKey = fs.readFileSync("/root/.ssh/id_rsa", ENCODE);
//以下部分可以用在server sidevar signer = crypto.createSign(alg);signer.update(n);var secrit = signer.sign(privKey, 'base64');console.log('SSH key singed...............................................');console.log(secrit);
//STEP5: 轉換過的pem檔案,就可以透過crypto進行verifyconsole.log('Compute SSH key to PEM.......................................');var pem = hs.sshKeyToPEM(pubKey);console.log(pem);
console.log('Start to verify..............................................');var ver = crypto.createVerify(alg);ver.update(n);var v = ver.verify(pem, secrit, 'base64');console.log(v); //這邊會顯示verify的結果