NODE.JS加密模塊CRYPTO常用方法介紹
使用require('crypto')調(diào)用加密模塊。
加密模塊需要底層系統(tǒng)提供OpenSSL的支持。它提供了一種安全憑證的封裝方式,可以用于HTTPS安全網(wǎng)絡(luò)以及普通HTTP連接。
該模塊還提供了一套針對OpenSSL的hash(哈希),hmac(密鑰哈希),cipher(編碼),decipher(解碼),sign(簽名)以及verify(驗證)等方法的封裝。
crypto.createCredentials(details)
創(chuàng)建一個憑證對象,可選參數(shù)details為一個帶鍵值的字典:
key:為字符串型,PEM編碼的私鑰。
cert:為字符串型,PEM編碼的認證證書。
ca:字符串形式的PEM編碼可信CA證書,或證書列表。
如果沒有給出'ca'的詳細內(nèi)容,那么node.js將會使用默認的公開受信任列表,該表位于http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt。
crypto.createHash(algorithm)
創(chuàng)建并返回一個hash對象,它是一個指定算法的加密hash,用于生成hash摘要。
參數(shù)algorithm可選擇系統(tǒng)上安裝的OpenSSL版本所支持的算法。例如:'sha1', 'md5', 'sha256', 'sha512'等。在近期發(fā)行的版本中,openssl list-message-digest-algorithms會顯示這些可用的摘要算法。
hash.update(data)
更新hash的內(nèi)容為指定的data。當(dāng)使用流數(shù)據(jù)時可能會多次調(diào)用該方法。
hash.digest(encoding='binary')
計算所有傳入數(shù)據(jù)的hash摘要。參數(shù)encoding(編碼方式)可以為'hex', 'binary' 或者'base64'。
crypto.createHmac(algorithm, key)
創(chuàng)建并返回一個hmac對象,它是一個指定算法和密鑰的加密hmac。
參數(shù)algorithm可選擇OpenSSL支持的算法 - 參見上文的createHash。參數(shù)key為hmac所使用的密鑰。
hmac.update(data)
更新hmac的內(nèi)容為指定的data。當(dāng)使用流數(shù)據(jù)時可能會多次調(diào)用該方法。
hmac.digest(encoding='binary')
計算所有傳入數(shù)據(jù)的hmac摘要。參數(shù)encoding(編碼方式)可以為'hex', 'binary' 或者'base64'。
crypto.createCipher(algorithm, key)
使用指定的算法和密鑰創(chuàng)建并返回一個cipher對象。
參數(shù)algorithm可選擇OpenSSL支持的算法,例如'aes192'等。在最近的發(fā)行版中,openssl list-cipher-algorithms會顯示可用的加密的算法。
cipher.update(data, input_encoding='binary', output_encoding='binary')
使用參數(shù)data更新要加密的內(nèi)容,其編碼方式由參數(shù)input_encoding指定,可以為 'utf8', 'ascii'或者'binary'。參數(shù)output_encoding指定了已加密內(nèi)容的輸出編碼方式,可以為 'binary', 'base64'或'hex'。
返回已加密的內(nèi)容,當(dāng)使用流數(shù)據(jù)時可能會多次調(diào)用該方法。
cipher.final(output_encoding='binary')
返回所有剩余的加密內(nèi)容,output_encoding輸出編碼為'binary', 'ascii'或'utf8'其中之一。
crypto.createDecipher(algorithm, key)
使用給定的算法和密鑰創(chuàng)建并返回一個解密對象。該對象為上述加密對象的反向運算。
decipher.update(data, input_encoding='binary', output_encoding='binary')
使用參數(shù)data更新要解密的內(nèi)容,其編碼方式為'binary','base64'或'hex'。參數(shù)output_encoding指定了已解密的明文內(nèi)容的輸出編碼方式,可以為 'binary','ascii'或'utf8'。
decipher.final(output_encoding='binary')
返回全部剩余的已解密的明文,其output_encoding' 為'binary', 'ascii'或'utf8'`其中之一。
crypto.createSign(algorithm)
使用給定的算法創(chuàng)建并返回一個簽名器對象。在現(xiàn)有的OpenSSL發(fā)行版中,openssl list-public-key-algorithms會顯示可用的簽名算法,例如:'RSA-SHA256'。
signer.update(data)
使用data參數(shù)更新簽名器對象。當(dāng)使用流數(shù)據(jù)時可能會多次調(diào)用該方法。
signer.sign(private_key, output_format='binary')
對所有傳入簽名器的數(shù)據(jù)計算其簽名。private_key為字符串,它包含了PEM編碼的用于簽名的私鑰。
返回簽名,其output_format輸出可以為'binary', 'hex' 或者'base64'。
crypto.createVerify(algorithm)
使用給定算法創(chuàng)建并返回一個驗證器對象。它是上述簽名器對象的反向運算。
verifier.update(data)
使用data參數(shù)更新驗證器對象。當(dāng)使用流數(shù)據(jù)時可能會多次調(diào)用該方法。
verifier.verify(cert, signature, signature_format='binary')
使用參數(shù)cert和signature驗證已簽名的數(shù)據(jù),cert為經(jīng)過PEM編碼的公鑰字符串,signature為之前已計算的數(shù)據(jù)的簽名,signature_format可以為'binary','hex' 或者'base64'。
根據(jù)對數(shù)據(jù)和公鑰進行簽名有效性驗證的結(jié)果,返回true或者false。
當(dāng)你需要一個不可逆的加密代碼如何寫
var text = "123|12312312123123121231231212312312123123121231231212312312";
var hasher=crypto.createHash("md5");
hasher.update(text);
var hashmsg=hasher.digest('hex');//hashmsg為加密之后的數(shù)據(jù)
當(dāng)你需要一個加密和解密的環(huán)境時
var key="asdhjwheru*asd123-123";//加密的秘鑰
var text = "123|12312312123123121231231212312312123123121231231212312312";
var crypted =cipher.update(text,'utf8','hex');
crypted+=cipher.final('hex');
var message=crypted;//加密之后的值
var decipher = crypto.createDecipher('aes-256-cbc',key);
var dec=decipher.update(message,'hex','utf8');
dec+= decipher.final('utf8');//解密之后的值
PS:關(guān)于加密技術(shù),本站還提供了如下加密工具供大家參考使用:
MD5在線加密工具:http://tools.jb51.net/password/CreateMD5Password
Escape加密/解密工具:http://tools.jb51.net/password/escapepwd
在線SHA1加密工具:http://tools.jb51.net/password/sha1encode
短鏈(短網(wǎng)址)在線生成工具:http://tools.jb51.net/password/dwzcreate
短鏈(短網(wǎng)址)在線還原工具:http://tools.jb51.net/password/unshorturl
高強度密碼生成器:http://tools.jb51.net/password/CreateStrongPassword
相關(guān)文章
koa-router源碼學(xué)習(xí)小結(jié)
這篇文章主要介紹了koa-router源碼學(xué)習(xí)小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09