crypto加密解密

加密Crypto#

使用require('crypto') 可以访问该模块。

加密模块要求底层系统的OpenSSL是支持的。它提供了一个安全证书,作为一个安全的HTTPS net或HTTP连接一部分用于封装方式

它还提供了一套OpenSSL的哈希,HMAC,加密,解密签名和验证方法包装

crypto.createCredentials(details)#

创建一个认证对象,detail是可选的钥(key)字典的参数。

如果没有具体的 'ca'给定,Node.js会使用以下给定的默认的公共可信赖的CA证书集合:

http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt.

crypto.createHash(algorithm)#

创建并返回一个加密哈希对象一个给定算法,可用于生成哈希摘要

algorithm 参数依赖于平台OpenSSL支持的可用算法。例如: 'sha1''md5''sha256''sha512', 等等。在最近的发布中, openssl list-message-digest-algorithms 会列出可用的摘要加密算法。

例子:使用sha1算法摘要一个文件

var filename = process.argv[2]; 
var crypto = require('crypto'); 
var fs = require('fs'); 
var shasum = crypto.createHash('sha1'); 
var s = fs.ReadStream(filename); 
s.on('data', function(d) { 
    shasum.update(d); 
}); 
s.on('end', function() { 
    var d = shasum.digest('hex'); 
   console.log(+ ' ' + filename); 
});

Class: Hash#

创建数据的hash摘要的类。

通过 crypto.createHash返回。

hash.update(data, [input_encoding])#

用给定的数据 data 更新hash摘要。 input_encoding 参数允许的编码为'utf8''ascii' 或 'binary'。默认为'binary'。这个方法可以多次调用(有新数据到达就可以调用) 。

hash.digest([encoding])#

计算已接收到数据的hash摘要。编码 encoding 可以是 'hex''binary' 或 'base64'。默认为 'binary'.

注意: hash 对象在调用过 digest() 方法后不能在使用。

crypto.createHmac(algorithm, key)#

创建并返回hmac对象,一个根据给定算法和密钥加密过的hmac。

 algorithm 参数依赖于平台OpenSSL支持的可用算法,详见上面的createHash。 key 参数是hmac用到的密钥。

Class: Hmac#

创建加密图形内容的类。

通过 crypto.createHmash返回。

hmac.update(data)#

用给定的数据 data 更新hash摘要。这个方法可以多次调用(有新数据到达就可以调用)

hmac.digest([encoding])#

计算已接收到数据的 hmac 摘要。编码 encoding 可以是 'hex''binary' 或 'base64'。默认为 'binary'.

注意: hmac 对象在调用过 digest() 方法后不能在使用。

crypto.createCipher(algorithm, password)#

根据给定的算法和密码,创建并返回加密对象。

algorithm 依赖于OpenSSL,例如:'aes192',等等。 在最近的发布中, openssl list-message-digest-algorithms 会列出可用的摘要加密算法。 password 用于派生密钥(key)和IV,必须是二进制 'binary' 编码字符串。(详见Buffer section )。

crypto.createCipheriv(algorithm, key, iv)#

根据给定的算法、密钥(key)和iv,创建并返回加密对象。

algorithm 和 createCipher()中的是一样的。 key 是在算法中使用的原密钥。 iv 是一个初始化的向量。 key 和iv 必须是二进制 'binary' 编码的字符串 (详见Buffer section )。

Class: Cipher#

加密数据的类。

通过 crypto.createCipher 和 crypto.createCipheriv返回。

cipher.update(data, [input_encoding], [output_encoding])#

使用 data更新cipher对象。 输入编码 input_encoding 可以是 'utf8','ascii' 或 'binary'。默认为 'binary'

输出编码 output_encoding 编码输出数据,可以是 'binary''base64' 或'hex'。默认为 'binary'

返回加密过的内容,这个方法可以多次调用(有新数据到达就可以调用)。

cipher.final([output_encoding])#

返回所有剩余加密过的内容,输出编码output_encoding 可以是:'binary''base64' 或'hex'。默认为'binary'

注意:cipher 对象在final() 方法调用后不能再使用。

crypto.createDecipher(algorithm, password)#

根据给定的算法和密码,创建并返解密对象。这个方法是上述createCipher()方法的镜像。

crypto.createDecipheriv(algorithm, key, iv)#

根据给定的算法、密钥(key)和iv,创建并返回解密对象。这个方法是上createCipheriv() 方法的镜像。

Class: Decipher#

解密数据的类。

通过 crypto.createDecipher 和 crypto.createDecipheriv返回。

decipher.update(data, [input_encoding], [output_encoding])#

使用 data更新decipher对象。 输入编码 input_encoding 可以是 'utf8','ascii' 或 'binary'。默认为 'binary'

输出编码 output_encoding 编码输出数据,可以是 'binary''base64' 或'hex'。默认为 'binary'

decipher.final([output_encoding])#

返回所有剩余加密过的内容,输出编码output_encoding 可以是:'binary''base64' 或'hex'。默认为'binary'

注意:cipher 对象在final() 方法调用后不能再使用。

crypto.createSign(algorithm)#

根据给定的算法,创建并返回一个签名对象。在最近的发布中, openssl list-message-digest-algorithms 会列出可用的摘要加密算法。例如: 'RSA-SHA256'

Class: Signer#

生成签名的类。

通过 crypto.createSign返回。

signer.update(data)#

以data数据更新签名对象。这个方法可以多次调用(有新数据到达就可以调用)。

signer.sign(private_key, [output_format])#

通过更新后的数据计算签名。private_key 是一个用于签名的PEM编码的私钥。

输出的签名编码 output_format 可以是 'binary''hex' 或 'base64'。默认为'binary'

注意:signer 对象在调用了sign方法后不能再使用。

crypto.createVerify(algorithm)#

根据给定的算法,创建并返回校验对象。这是签名方法的镜像。

Class: Verify#

校验签名的类。

通过 crypto.createVerify返回。

verifier.update(data)#

以data数据更新校验对象。这个方法可以多次调用(有新数据到达就可以调用)。

verifier.verify(object, signature, [signature_format])#

使用 object 和 signature校验签名。 object 是一个包含PEM编码的字符串,可以是RSA的公钥,DSA公钥或 X.509证书。 signature 之前通过data数据计算出来的签名, signature_format 可以是 'binary''hex' 或 'base64'。默认为 'binary'

返回值的真假取决于数据的签名和公钥的有效性。

注意:verifier 对象在调用 verify() 方法后不能再使用。

crypto.createDiffieHellman(prime_length)#

创建一个Diffie-Hellman密钥交换对象,并生成一个给定的位长度的prime。常用 2

crypto.createDiffieHellman(prime, [encoding])#

使用一个给定的prime,创建一个Diffie-Hellman密钥交换对象。常用 2。编码可以是 'binary''hex'或 'base64' 。默认为 'binary'

Class: DiffieHellman#

创建Diffie-Hellman密钥交换对象的类。

通过 crypto.createDiffieHellman返回。

diffieHellman.generateKeys([encoding])#

生成私有或公有的Diffie-Hellman密钥,返回给定编码的公钥。这个密钥会转换为其他部分。编码可以是 'binary''hex'或 'base64'。默认为 'binary'

diffieHellman.computeSecret(other_public_key, [input_encoding], [output_encoding])#

使用 other_public_key 为公钥,计算共享密钥,返回密钥。  other_public_key  以  input_encoding 编码, 输出的密钥以output_encoding编码。 编码可以是 'binary''hex', 或 'base64'。输入编码默认为'binary'。如果输出编码没有指定,则默认使用输入编码作为输出编码。

diffieHellman.getPrime([encoding])#

以encoding编码格式返回Diffie-Hellman的prime。编码可以是 'binary''hex', 或 'base64'。默认为'binary'

diffieHellman.getGenerator([encoding])#

以encoding编码格式返回Diffie-Hellman的prime。编码可以是 'binary''hex', 或 'base64'。默认为'binary'

diffieHellman.getPublicKey([encoding])#

以encoding编码格式返回Diffie-Hellman的公钥。编码可以是 'binary''hex', 或 'base64'。默认为'binary'

diffieHellman.getPrivateKey([encoding])#

以encoding编码格式返回Diffie-Hellman的私钥。编码可以是 'binary''hex', 或 'base64'。默认为'binary'

diffieHellman.setPublicKey(public_key, [encoding])#

以encoding编码格式设置Diffie-Hellman的公钥。编码可以是 'binary''hex', 或 'base64'。默认为'binary'

diffieHellman.setPrivateKey(public_key, [encoding])#

以encoding编码格式设置Diffie-Hellman的私钥。编码可以是 'binary''hex', 或 'base64'。默认为'binary'

crypto.pbkdf2(password, salt, iterations, keylen, callback)#

异步的PBKDF2提供HMAC-SHA1伪函数,从而从给定的password,salt,iterations派生出密钥(key)。回调函数有 (err, derivedKey)两个参数。

crypto.randomBytes(size, [callback])#

生成伪随机强加密数据。使用方式:

// async 
crypto.randomBytes(256, function(ex, buf) { 
    if (ex) throw ex; 
   console.log('Have %d bytes of random data: %s', buf.length, buf); 
}); 
// sync 
try { 
    var buf = crypto.randomBytes(256); 
   console.log('Have %d bytes of random data: %s', buf.length, buf); 
} catch (ex) { 
    // handle error 
}

转载于:https://my.oschina.net/chinacaptain/blog/304772

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Crypto API 是一个通用的加密API,可用于实现各种加密和解密算法。下面是一个使用 Crypto API 进行加密解密的示例: ```python import hashlib from Crypto.Cipher import AES # 对称加密算法 class SymmetricEncryption: def __init__(self, key): self.key = key # 加密函数 def encrypt(self, data): cipher = AES.new(self.key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(data.encode('utf-8')) return (nonce + ciphertext + tag) # 解密函数 def decrypt(self, data): nonce = data[:AES.block_size] tag = data[-16:] ciphertext = data[AES.block_size:-16] cipher = AES.new(self.key, AES.MODE_EAX, nonce=nonce) plaintext = cipher.decrypt(ciphertext) try: cipher.verify(tag) return plaintext.decode('utf-8') except: return None # 哈希算法 class HashAlgorithm: def __init__(self, algorithm): self.algorithm = algorithm # 计算哈希值 def hash(self, data): h = hashlib.new(self.algorithm) h.update(data.encode('utf-8')) return h.hexdigest() ``` 上面的代码定义了两个类:`SymmetricEncryption` 和 `HashAlgorithm`。 `SymmetricEncryption` 类使用 AES 对称加密算法进行加密和解密。 `HashAlgorithm` 类使用哈希算法计算哈希值。 使用示例: ```python # 对称加密示例 key = b'Sixteen byte key' encryptor = SymmetricEncryption(key) encrypted_data = encryptor.encrypt('hello world') print(encrypted_data) decrypted_data = encryptor.decrypt(encrypted_data) print(decrypted_data) # 哈希算法示例 hasher = HashAlgorithm('sha256') hashed_data = hasher.hash('hello world') print(hashed_data) ``` 在上面的示例中,我们使用了 AES 对称加密算法和 SHA256 哈希算法对数据进行了加密和哈希。在实际应用中,我们可以根据需要选择不同的加密和哈希算法来保护数据的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值