@signumjs/crypto
Version:
Cryptographic functions for building Signum Network apps.
68 lines • 2.06 kB
JavaScript
;
/**
* Original work Copyright (c) 2024 Signum Network
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.sha256Raw = sha256Raw;
exports.sha256AsBytes = sha256AsBytes;
exports.sha256AsHex = sha256AsHex;
exports.sha256AsBase64 = sha256AsBase64;
exports.sha256Binary = sha256Binary;
const base_1 = require("./base");
/**
* Hash string into raw array buffer
* @param input An arbitrary text
* @param encoding The encoding of input type (default: 'utf8')
* @return the hash for that string in ArrayBuffer
*
* @category sha256
*/
function sha256Raw(input, encoding) {
return base_1.Crypto.adapter.sha256(base_1.Buffer.from(input, encoding));
}
/**
* Hash string into byte array
* @param input An arbitrary text
* @param encoding The encoding of input type (default: 'utf8')
* @return the hash for that string in Uint8Array
*
*
* @category sha256
*/
function sha256AsBytes(input, encoding = 'utf8') {
return new Uint8Array(sha256Raw(input, encoding));
}
/**
* Hash string into hex string
* @param input An arbitrary text (utf-8)
* @param encoding The encoding of input type (default: 'utf8')
* @return the hash for that string in hex format
*
* @category sha256
*/
function sha256AsHex(input, encoding = 'utf8') {
return base_1.Buffer.from(sha256Raw(input, encoding)).toString('hex');
}
/**
* Hash string into base64 string
* @param input An arbitrary text (utf-8)
* @param encoding The encoding of input type (default: 'utf8')
* @return the hash for that string in base64 format
*
* @category sha256
*/
function sha256AsBase64(input, encoding = 'utf8') {
return base_1.Buffer.from(sha256Raw(input, encoding)).toString('base64');
}
/**
* Hashes binary data into byte array
* @param data A hex data string or byte array
* @return the hash for that as byte array
*
* @category sha256
*/
function sha256Binary(data) {
const b = typeof data === 'string' ? base_1.Buffer.from(data, 'hex') : data;
return base_1.Crypto.adapter.sha256(b);
}
//# sourceMappingURL=sha256.js.map