如何在Node JS中生成HEX编码的CMAC-AES摘要?

时间:2014-11-11 09:48:40

标签: javascript node.js encryption cryptography oauth-2.0

我需要使用Node.JS生成HEX编码的CMAC-AES摘要。我找到了library from GitHub。我想知道怎么称呼它? This是我想在node.js中完成的部分。我想通过keymessage

我将这个库导入我的node.js项目。我想知道如何调用此方法aesCmac

我的index.js

var express = require('express');
var querystring = require('querystring');
var http = require('http');
var aesCmac = require('./lib/aes-cmac.js');

var app = express();

app.get('/aesCmac', aesCmac.aesCmac('xxxxxxxx' ,'LSCourse|0xxxxxxxx|103xxxxxxxx|xxxxx|xxxx.xxx.xx@xxx.com|2014-11-11T09:29:04Z'));

app.configure(function () {
    app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
    app.use(express.bodyParser());
});

app.listen(80);

我收到了这个错误:

F:\NODE.JS\node-aes-cmac-master>node index.js
node-crypto : Invalid key length 32

crypto.js:315
  this._binding.initiv(cipher, toBuf(key), toBuf(iv));
                ^
Error: error:0607A082:digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length
    at new Cipheriv (crypto.js:315:17)
    at Object.Cipheriv (crypto.js:313:12)
    at aes128 (F:\NODE.JS\node-aes-cmac-master\lib\aes-cmac.js:25:23)
    at Object.exports.generateSubkeys (F:\NODE.JS\node-aes-cmac-master\lib\aes-cmac.js:9:11)
    at Object.exports.aesCmac (F:\NODE.JS\node-aes-cmac-master\lib\aes-cmac.js:32:25)
    at Object.<anonymous> (F:\NODE.JS\node-aes-cmac-master\index.js:9:29)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

我是Node js的新手。

1 个答案:

答案 0 :(得分:2)

test file显示了如何使用它。您不能简单地将aesCmac.aesCmac插入app.get。您需要处理请求并发送响应。

// some hex encoded 512bit key
var key = new Buffer('2b7e151628aed2a6abf7158809cf4f3c', 'hex');
var msg = new Buffer('LSCourse|0xxxxxxxx|103xxxxxxxx|xxxxx|xxxx.xxx.xx@xxx.com|2014-11-11T09:29:04Z');

app.get('/aesCmac', function(req, res){
  var result = aesCmac.aesCmac(key, msg);
  res.status(200).send(result.toString('hex'));
});