NODEJS(11)Platform - AUTH- AES
1. AES - Introduction
DES-3DES-AES (Advanced Encryption Standard)
First to know
HEX is twice length of normal string. For example>
http://www.string-functions.com/string-hex.aspx
string(string) ——> HEX(737472696e67)
Second to know
List all the algorithms we support
>openssl list-cipher-algorithms
2. AES - Implementation
sillycat-aes/lib/app.js
var crypto = require('crypto');
module.exports = {
defaultAlgorithm: 'aes-256-cbc',
defaultFormat: 'hex',
ivLength: 16,
encrypt : function(data, key){
algorithm = this.defaultAlgorithm;
format = this.defaultFormat;
var iv = crypto.randomBytes(this.ivLength);
var cipher = crypto.createCipheriv(algorithm, new Buffer(key, 'hex'), iv);
var encrypted = cipher.update(data, 'utf8', format) + cipher.final(format);
//put the vector at the beginning
return iv.toString('hex') + encrypted;
},
decrypt : function(data, key){
algorithm = this.defaultAlgorithm;
format = this.defaultFormat;
var ivLength = this.ivLength * 2;
var iv = new Buffer(data.substring(0, ivLength), 'hex');
//get the vector from the begging of the string, hex is twice length the string
data = data.substring(ivLength);
var decipher = crypto.createDecipheriv(algorithm, new Buffer(key, 'hex'), iv);
var decrypted = decipher.update(data, format, 'utf8') + decipher.final('utf8');
return decrypted;
}
};
Here is the test class
sillcat-aes/test/encryptAndDecrypt.js
require("should");
var crypto = require('crypto');
var AESTool = require("../lib/app");
describe("Encrypt&Decrypt String", function(){
it("should encrypt&decrypt string password", function(){
console.log("");
var key = crypto.randomBytes(32);
var raw_data = "I love nodejs, I used to use javascript for a long time!";
var secret_data = AESTool.encrypt(raw_data, key);
console.log("raw_data is = " + raw_data);
console.log("secret data is = " + secret_data);
var decrypt_data = AESTool.decrypt(secret_data, key);
console.log("decrypt data is= " + decrypt_data);
decrypt_data.should.equal(raw_data);
});
});
3. Build the Test Case
>sudo npm install -g mocha
global install the mocha tool in my local machine.
value.should.be.a(‘object’).and.have.property(’name’, ‘Pedro’);
true.shuld.be.ok
false.should.ot.be.ok;
15.should.be.within(10,20);
“502”.should.match(/[0-9]{3}/);
[1,2,3].should.have.length(3);
“abcdef”.should.include.string(‘bc’);
{a:1,b:2}.should.be.a(‘object’);
“test”.should.be.a(“string");
Then, try to run the test cases like this>
>mocha test/*.js
raw_data is = I love nodejs, I used to use javascript for a long time! secret data is = 39f8bdc4e8087fd4c53195702522db82c5bf60771fedfa765ea8b7d7ea6a69c29f09054a13c1a1e4d1ae52e285ebdc5ea62fe649e64781888626febfc384a3c3c7e306ae319ed586892009fc6596a890 decrypt data is= I love nodejs, I used to use javascript for a long time!
References:
AES Encrypt and Decrypt
http://blog.csdn.net/searchsun/article/details/2516191
http://nodejs.org/api/crypto.html
http://www.360doc.com/content/10/0326/17/229143_20367618.shtml
http://snoopyxdy.blog.163.com/blog/static/601174402012730105523656/
http://sillycat.iteye.com/blog/1468900
Dependency Injection
https://github.com/harmonyjs/dependency-injection
- 浏览: 2560793 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 484NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 345Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 444Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 394Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 488NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 432Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 342Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 255GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 456GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 332GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 318Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 324Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 302Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 315Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 299NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 266Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 579NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 273Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 382Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 382Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
nodejs-auth-master.rar
NodeJS-GraphQL-Auth-Starter 使用JWT使用NodeJ和GraphQL进行用户身份验证的入门项目。 入门 这些说明将为您提供在本地计算机上运行并运行的项目的副本,以进行开发和测试。 本地设置 一系列循序渐进的示例,告诉您...
项目名称 具有JWT身份验证支持的Node.JS API 入门 这些说明将为您提供在本地计算机上运行并运行的项目的副本,以进行开发...cd nodejs-api-auth-with-jwt # install node_modules npm install # run node app.js 您的节
基于vue+nodejs毕业设计-在线购物商城系统(源码+数据库+文档说明)含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用,该项目可以作为毕设、期末大作业使用,...
基于vue+nodejs毕业设计-图书管理系统(源码+论文资料),该项目是个人毕设项目,答辩评审分达到98分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。该资源主要针对计算机、通信、人工...
cos-nodejs-sdk-v5腾讯云 COS Nodejs SDK()installnpm i cos-nodejs-sdk-v5 --savedemo// 引入模块var COS = require('cos-nodejs-sdk-v5');// 创建实例var cos = new COS({ SecretId: '...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs010-nodejs-request-2.25.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-minimatch-0.2.14-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-sigmund-1.0.0-6.el6.centos.alt.noarch.rpm
nodejs010-nodejs-mkdirp-0.3.5-3.el6.centos.alt.noarch.rpm
nodejs010-nodejs-nan-0.4.4-2.2.el6.centos.alt.noarch.rpm
nodejs010-nodejs-hawk-1.0.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-rimraf-2.2.6-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-sntp-0.2.4-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-inherits-2.0.0-6.el6.centos.alt.noarch.rpm
nodejs010-nodejs-mime-1.2.11-3.el6.centos.alt.noarch.rpm