@govtechsg/open-attestation
Version:
58 lines (57 loc) • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeSalt = exports.encodeSalt = exports.salt = exports.secureRandomString = void 0;
var crypto_1 = require("crypto");
var js_base64_1 = require("js-base64");
var traverseAndFlatten_1 = require("./traverseAndFlatten");
var ENTROPY_IN_BYTES = 32;
var illegalCharactersCheck = function (data) {
Object.entries(data).forEach(function (_a) {
var key = _a[0], value = _a[1];
if (key.includes(".")) {
throw new Error("Key names must not have . in them");
}
if (key.includes("[") || key.includes("]")) {
throw new Error("Key names must not have '[' or ']' in them");
}
if (value && typeof value === "object") {
return illegalCharactersCheck(value); // Recursively search if property contains sub-properties
}
});
};
// Using 32 bytes of entropy as compared to 16 bytes in uuid
// Using hex encoding as compared to base64 for constant string length
/**
* @deprecated will be removed in the next major release in favour of OpenAttestation v4.0 (more info: https://github.com/Open-Attestation/open-attestation/tree/alpha)
*/
var secureRandomString = function () { return (0, crypto_1.randomBytes)(ENTROPY_IN_BYTES).toString("hex"); };
exports.secureRandomString = secureRandomString;
/**
* @deprecated will be removed in the next major release in favour of OpenAttestation v4.0 (more info: https://github.com/Open-Attestation/open-attestation/tree/alpha)
*/
var salt = function (data) {
// Check for illegal characters e.g. '.', '[' or ']'
illegalCharactersCheck(data);
return (0, traverseAndFlatten_1.traverseAndFlatten)(data, { iteratee: function (_a) {
var path = _a.path;
return ({ value: (0, exports.secureRandomString)(), path: path });
} });
};
exports.salt = salt;
/**
* @deprecated will be removed in the next major release in favour of OpenAttestation v4.0 (more info: https://github.com/Open-Attestation/open-attestation/tree/alpha)
*/
var encodeSalt = function (salts) { return js_base64_1.Base64.encode(JSON.stringify(salts)); };
exports.encodeSalt = encodeSalt;
/**
* @deprecated will be removed in the next major release in favour of OpenAttestation v4.0 (more info: https://github.com/Open-Attestation/open-attestation/tree/alpha)
*/
var decodeSalt = function (salts) {
var decoded = JSON.parse(js_base64_1.Base64.decode(salts));
decoded.forEach(function (salt) {
if (salt.value.length !== ENTROPY_IN_BYTES * 2)
throw new Error("Salt must be ".concat(ENTROPY_IN_BYTES, " bytes"));
});
return decoded;
};
exports.decodeSalt = decodeSalt;