@intuitionrobotics/ts-common
Version:
50 lines • 2.02 kB
JavaScript
/*
* ts-common is the basic building blocks of our typescript projects
*
* Copyright (C) 2020 Intuition Robotics
*
* 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.
*/
// node-forge is CJS; named imports from it don't work under Node's ESM-CJS
// interop because forge exposes `md` as a property of the default export,
// not as a statically-detectable named export.
import forge from "node-forge";
export function md5(toBeConverted) {
return forge.md.md5.create().update(toBeConverted).digest().toHex().toLowerCase();
}
export function sha1(toBeConverted) {
return forge.md.sha1.create().update(toBeConverted).digest().toHex().toLowerCase();
}
export function sha256(toBeConverted) {
return forge.md.sha256.create().update(toBeConverted).digest().toHex().toLowerCase();
}
export function sha384(toBeConverted) {
return forge.md.sha384.create().update(toBeConverted).digest().toHex().toLowerCase();
}
export function sha512(toBeConverted) {
return forge.md.sha512.create().update(toBeConverted).digest().toHex().toLowerCase();
}
export function encode(data, encoding = "base64") {
let buffer;
if (Buffer.isBuffer(data))
buffer = data;
else if (typeof data === 'string')
buffer = Buffer.from(data, 'utf8');
else
buffer = Buffer.from(data.toString(), 'utf8');
return buffer.toString(encoding);
}
export function decode(encoded, from = "base64", to = "utf8") {
return Buffer.from(encoded, from).toString(to);
}
//# sourceMappingURL=hash-tools.js.map