@ar.io/sdk
Version:
[](https://codecov.io/gh/ar-io/ar-io-sdk)
57 lines (56 loc) • 2.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.hashBufferToB64Url = exports.hashReadableStreamToB64Url = exports.hashReadableToB64Url = void 0;
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
const crypto_1 = require("crypto");
const base64_js_1 = require("./base64.js");
const hashReadableToB64Url = (stream, algorithm = 'sha256') => {
return new Promise((resolve, reject) => {
const hash = (0, crypto_1.createHash)(algorithm);
stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => resolve((0, base64_js_1.toB64Url)(hash.digest())));
stream.on('error', (err) => reject(err));
});
};
exports.hashReadableToB64Url = hashReadableToB64Url;
const hashReadableStreamToB64Url = (stream, algorithm = 'sha256') => {
return new Promise((resolve, reject) => {
const hash = (0, crypto_1.createHash)(algorithm);
const reader = stream.getReader();
const read = async () => {
try {
const { done, value } = await reader.read();
if (done) {
resolve((0, base64_js_1.toB64Url)(hash.digest()));
}
else {
hash.update(value);
read();
}
}
catch (err) {
reject(err);
}
};
read().catch(reject);
});
};
exports.hashReadableStreamToB64Url = hashReadableStreamToB64Url;
const hashBufferToB64Url = (buffer, algorithm = 'sha256') => {
return (0, base64_js_1.toB64Url)((0, crypto_1.createHash)(algorithm).update(buffer).digest());
};
exports.hashBufferToB64Url = hashBufferToB64Url;
;