@n3okill/utils
Version:
Many javascript helpers
51 lines • 2.39 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateData = validateData;
const NodeCrypto = __importStar(require("crypto"));
/**
* Validates if the given data is tampered.
* @param data Data to be validated. The data must be previously
* generated by `[hashData()]`.
* @param key Secret key that was previously used to generate the hash for the data in `[hashData()]`.
* function to see the supported hashing algorithms on your system. This must be the same
* as the value passed to `[hashData()]` when generating the hash for the data.
* @param algorithm Algorithm used to generate the data
* @return the real data with the hash stripped off. False if the data is tampered.
* @see hashData()
*/
function validateData(data, key, algorithm = "sha256") {
const hashLength = NodeCrypto.createHmac(algorithm, key).update("test").digest("base64").length;
if (data.length >= hashLength) {
const hash = data.substring(0, hashLength);
const pureData = data.substring(hashLength, data.length);
const calculatedHash = NodeCrypto.createHmac(algorithm, key).update(pureData).digest("base64");
if (NodeCrypto.timingSafeEqual(Buffer.from(hash), Buffer.from(calculatedHash))) {
return pureData;
}
}
return false;
}
//# sourceMappingURL=validateData.js.map
;