@softchef/cdk-iot-device-management
Version:
IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.
31 lines (30 loc) • 1.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hash = void 0;
const util_buffer_from_1 = require("@aws-sdk/util-buffer-from");
const buffer_1 = require("buffer");
const crypto_1 = require("crypto");
class Hash {
constructor(algorithmIdentifier, secret) {
this.hash = secret ? crypto_1.createHmac(algorithmIdentifier, castSourceData(secret)) : crypto_1.createHash(algorithmIdentifier);
}
update(toHash, encoding) {
this.hash.update(castSourceData(toHash, encoding));
}
digest() {
return Promise.resolve(this.hash.digest());
}
}
exports.Hash = Hash;
function castSourceData(toCast, encoding) {
if (buffer_1.Buffer.isBuffer(toCast)) {
return toCast;
}
if (typeof toCast === "string") {
return util_buffer_from_1.fromString(toCast, encoding);
}
if (ArrayBuffer.isView(toCast)) {
return util_buffer_from_1.fromArrayBuffer(toCast.buffer, toCast.byteOffset, toCast.byteLength);
}
return util_buffer_from_1.fromArrayBuffer(toCast);
}