ObjectDigest
Information summary object
You can use the Digest object like this:
1
2
3
4
5
6
7
8
9
10
11
12
13const crypto = require('crypto');
// create a SHA-512 digest object
const digest = crypto.createHash('sha512');
// update digest with data
digest.update('hello');
digest.update('world');
// get digest result
const result = digest.digest();
console.log(result);
// output result in hex and base64
console.log(result.toString('hex'));
console.log(result.toString('base64'));
In the above code, crypto.createHash()
a SHA-512 digest operation object is created using the method. update()
The data to be summarized can be gradually added through the method, and digest()
the summary result can be obtained through the method.
inheritance relationship
member properties
size
Integer, query the number of digest bytes of the current information digest algorithm
1readonly Integer Digest.size;
member function
update
Update binary digest information
1Digest Digest.update(Buffer data);
Call parameters:
- data:Buffer, binary data block
Return results:
- Digest, returns the information summary object itself
Update string summary information
1
2Digest Digest.update(String data,
String codec = "utf8");
Call parameters:
- data: String, string data
- codec: String, specifies the encoding format, allowed values are: "buffer", "hex", "base32", "base58", "base64", "utf8", oriconvCharacter sets supported by the module
Return results:
- Digest, returns the information summary object itself
digest
Compute and return summary
1Value Digest.digest(String codec = "buffer");
Call parameters:
- codec: String, specifies the encoding format, allowed values are: "buffer", "hex", "base32", "base58", "base64", "utf8", oriconvCharacter sets supported by the module
Return results:
- Value, returns a summary representation of the specified encoding
sign
Sign and return summary
1
2Buffer Digest.sign(PKey key,
Object opts = {}) async;
Call parameters:
- key:PKey, signed private key
- opts: Object, specify signature options
Return results:
- Buffer, return the signed data
opts supports the following parameters:
1
2
3
4
5
6
7
8
9
10{
alg: 0 指定签名的 hash 算法, 仅在 RSA 时有效, 缺省为 0. 支持算法: 0 = NONE,
1 = MD5,
2 = SHA1,
3 = SHA224,
4 = SHA256,
5 = SHA384,
6 = SHA512,
7 = RIPEMD160
}
verify
Verify signatures are consistent
1
2
3Boolean Digest.verify(PKey key,
Buffer sign,
Object opts = {}) async;
Call parameters:
- key:PKey, the public key to verify the signature
- sign:Buffer, specify the signature to be verified
- opts: Object, specify verification options
Return results:
- Boolean, returns the verified result
opts supports the following parameters:
1
2
3
4
5
6
7
8
9
10{
alg: 0 指定签名的 hash 算法, 仅在 RSA 时有效, 缺省为 0. 支持算法: 0 = NONE,
1 = MD5,
2 = SHA1,
3 = SHA224,
4 = SHA256,
5 = SHA384,
6 = SHA512,
7 = RIPEMD160
}
toString
Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String Digest.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value Digest.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable