[TOC]
### crypto 加密實例代碼
```
"use strict";
//引用crypto模塊
const crypto = require("crypto");
//-------------MD5 可以任意多次調用update(),update()默認字符串編碼是UTF-8
const hash = crypto.createHash("md5");
hash.update("hello, world!");
console.log(hash.digest("hex"));
//--------------SHA1
const sha1 = crypto.createHash("sha1");
sha1.update("hello,world!");
console.log(sha1.digest("hex"));
//-------------SHA256
const sha256 = crypto.createHash("sha256");
sha256.update("hello,world!");
console.log(sha256.digest("hex"));
//------------SHA512
const sha512 = crypto.createHash("sha512");
sha512.update("hello,world!");
console.log(sha512.digest("hex"));
//------------Hmac
const hmac = crypto.createHash("sha256","secret-key");
hmac.update("hello world!");
console.log(hmac.digest("hex"));
//-----------AES
function aesEncrypt(data, key) {
const cipher = crypto.createCipher("aes192", key);
var crypted = cipher.update(data, "utf8", "hex");
crypted += cipher.final("hex");
return crypted;
}
function aesDecrypt(encrypted, key) {
const decipher = crypto.createDecipher("aes192", key);
var decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
}
var data = "hello this a secret message!";
var key = "password";
var encrypted = aesEncrypt(data,key);
var decrypted = aesDecrypt(encrypted, key);
console.log("plain text:" + data);
console.log("Encrypted text:" +encrypted);
console.log(("Decrypted text:" + decrypted));
//------------------diffie-hellman
var ming = crypto.createDiffieHellman(512);
var ming_keys = ming.generateKeys();
var prime = ming.getPrime();
var generator = ming.getGenerator();
console.log("Prime:" + prime.toString("hex"));
console.log("Generator:" + generator.toString("hex") );
var hong = crypto.createDiffieHellman(prime, generator);
var hong_keys = hong.generateKeys();
var ming_secret = ming.computeSecret(hong_keys);
var hong_secret = hong.computeSecret(ming_keys);
console.log("Secret of xiao ming:" + ming_secret.toString("hex"));
console.log("Secret of xiao hong:" + hong_secret.toString("hex"));
//----------RSA
const fs = require("fs");
//從文件加載key
function loadKey(file) {
return fs.readFileSync(file,"utf8");
};
let prvKey = loadKey("./rsa-prv.pem");
let pubKey = loadKey("./rsa-pub.pem");
let message = "hello world!";
//使用私鑰加密公鑰解密
let enc_by_prv = crypto.privateEncrypt(prvKey,Buffer.from(message,"utf8"));
console.log("encrypted by private key:" + enc_by_prv.toString("hex"));
let dec_by_pub = crypto.publicDecrypt(pubKey, enc_by_prv);
console.log("decrypted by public key:" + dec_by_pub.toString("utf8"));
//使用公鑰進行加密私鑰解密
let enc_by_pub = crypto.publicEncrypt(pubKey,Buffer.from(message,"utf8"));
console.log("encrypted by public key:" + enc_by_pub.toString("hex"));
let dec_by_prv = crypto.privateDecrypt(prvKey,enc_by_pub);
console.log("decrypted by private key:" + dec_by_prv.toString("utf8"));
```
- 概述
- 起步
- 跨域配置
- 路徑別名
- 路由
- api版本控制
- 錯誤和異常
- 全局異常處理
- 數據庫
- 創建遷移文件
- sequelize數據類型
- 配置
- 新增
- 查詢
- 條件查詢
- 模糊查詢
- 排序查詢
- 聚合查詢
- 分組查詢
- 分頁查詢
- 修改
- 刪除
- 獲取器
- 修改器
- 靜態屬性
- 字段驗證
- 外鍵約束
- 關聯模型
- 一對一
- 一對多
- 左外連接
- 多對多
- 字段顯示隱藏
- 事務
- 字段自增
- 驗證層
- egg-validate
- indicative驗證器
- egg-validate-plus
- betterValidate
- 校驗規則
- 中間件
- 安全
- 數據加密
- 單向加密
- 示例代碼
- 封裝egg加密
- 上傳
- path模塊
- 單文件上傳
- 多文件上傳
- 按照日期存儲
- 工具函數
- egg常用工具函數
- 緩存
- 配置緩存插件
- 設置緩存
- 獲取緩存
- 刪除緩存
- 消息隊列
- rabbitMQ
- 安裝
- 簡單隊列
- 工作隊列
- 工作隊列(dispach分發)
- 消息應答和持久化
- redis
- 數據類型
- 字符串類型(String)
- 哈希類型(Hash)
- 列表(List)
- 無序集合(Set)
- 可排序集合(Zset)
- 郵件系統
- nodeMailer
- 第三方模塊
- 生成隨機數
- JWT
- JWT鑒權
- 生成Token
- 短信服務
- 阿里大魚短信驗證碼
- 發送短信邏輯
- 阿里短信Node類