@ton/crypto
Version:
[](https://www.npmjs.com/package/@ton/crypto)
39 lines (38 loc) • 1.01 kB
JavaScript
/**
* Copyright (c) Whales Corp.
* All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.bitsToBytes = exports.bytesToBits = exports.lpad = void 0;
function lpad(str, padString, length) {
while (str.length < length) {
str = padString + str;
}
return str;
}
exports.lpad = lpad;
function bytesToBits(bytes) {
let res = '';
for (let i = 0; i < bytes.length; i++) {
let x = bytes.at(i);
res += lpad(x.toString(2), '0', 8);
}
return res;
}
exports.bytesToBits = bytesToBits;
function bitsToBytes(src) {
if (src.length % 8 !== 0) {
throw Error('Uneven bits');
}
let res = [];
while (src.length > 0) {
res.push(parseInt(src.slice(0, 8), 2));
src = src.slice(8);
}
return Buffer.from(res);
}
exports.bitsToBytes = bitsToBytes;
;