UNPKG

aws-crt

Version:

NodeJS bindings to the aws-c-* libraries

111 lines 4.32 kB
"use strict"; /* * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const Crypto = __importStar(require("crypto-js")); const util_1 = require("util"); class Md5Hash { update(data) { this.hash = Crypto.MD5(data.toString(), this.hash ? this.hash.toString() : undefined); } finalize(truncate_to) { const digest = this.hash ? this.hash.toString() : ''; const truncated = digest.substring(0, truncate_to ? truncate_to : digest.length); const encoder = new util_1.TextEncoder(); const bytes = encoder.encode(truncated); return new DataView(bytes.buffer); } } exports.Md5Hash = Md5Hash; /** * Computes an MD5 hash. Use this if you don't need to stream the data you're hashing and can load the entire input * into memory. * * @param data The data to hash * @param truncate_to The maximum number of bytes to receive. Leave as undefined or 0 to receive the entire digest. */ function hash_md5(data, truncate_to) { const md5 = new Md5Hash(); md5.update(data); return md5.finalize(truncate_to); } exports.hash_md5 = hash_md5; class Sha256Hash { update(data) { this.hash = Crypto.SHA256(data.toString(), this.hash ? this.hash.toString() : undefined); } finalize(truncate_to) { const digest = this.hash ? this.hash.toString() : ''; const truncated = digest.substring(0, truncate_to ? truncate_to : digest.length); const encoder = new util_1.TextEncoder(); const bytes = encoder.encode(truncated); return new DataView(bytes.buffer); } } exports.Sha256Hash = Sha256Hash; /** * Computes an SHA256 hash. Use this if you don't need to stream the data you're hashing and can load the entire input * into memory. * * @param data The data to hash * @param truncate_to The maximum number of bytes to receive. Leave as undefined or 0 to receive the entire digest. */ function hash_sha256(data, truncate_to) { const digest = Crypto.SHA256(data.toString()).toString(); const truncated = digest.substring(0, truncate_to ? truncate_to : digest.length); const encoder = new util_1.TextEncoder(); const bytes = encoder.encode(truncated); return new DataView(bytes.buffer); } exports.hash_sha256 = hash_sha256; class Sha256Hmac { constructor(secret) { // @ts-ignore types file doesn't have this signature of create() this.hmac = Crypto.algo.HMAC.create(Crypto.algo.SHA256, secret); } update(data) { this.hmac.update(data.toString()); } finalize(truncate_to) { const digest = this.hmac.finalize(); const truncated = digest.toString().substring(0, truncate_to ? truncate_to : digest.length); const encoder = new util_1.TextEncoder(); const bytes = encoder.encode(truncated); return new DataView(bytes.buffer); } } exports.Sha256Hmac = Sha256Hmac; /** * Computes an SHA256 HMAC. Use this if you don't need to stream the data you're hashing and can load the entire input * into memory. * * @param secret The key to use for the HMAC process * @param data The data to hash * @param truncate_to The maximum number of bytes to receive. Leave as undefined or 0 to receive the entire digest. */ function hmac_sha256(secret, data, truncate_to) { const hmac = new Sha256Hmac(secret); hmac.update(data); return hmac.finalize(truncate_to); } exports.hmac_sha256 = hmac_sha256; //# sourceMappingURL=crypto.js.map