simple-binary-utils v1.1.1
Simple Binary Utils
This is a package that helps work with binary string representations of numbers and converted strings. You can convert any number or any text to binary and perform safe math operations on it and get outputs, thus resulting in a pseudo-cryptographic method of encoding your data. This package was designed as a helper for implementing the Fiege-Fiat-Shamir algorithm for text instead of numbers (thereby allowing conversion of strings into integer representations, performing math, and getting a different string result for the proof). Written using typescript.
Functions
Utilities
BinaryUtils.isBinaryString(str: string): string
: Determine if a string is a proper binary string.
BinaryUtils.fixLength(str: string): string
: Fixes a binary string by left-padding it with the appropriate number of 0's to complete a binary word.
BinaryUtils.findBestLength(...strs: string[]): number
: Finds the best length for all of the strings to have (the longest of the supplied parameters).
BinaryUtils.toBinary(a: number): string
: Converts a number to binary.
BinaryUtils.toDecimal(a: string): number
: Converts a binary string back to a decimal.
BinaryUtils.stringToBinary(str: string): string
: Converts a regular text string into a binary representation.
BinaryUtils.binaryToString(str: string): string
: Converts a binary string back into it's text representation.
Math
BinaryUtils.add(a: string, b: string): string
: Adds two binary strings and returns the resulting binary string
BinaryUtils.subtract(a: string, b: string): string
: Subtracts two binary strings and returns the resulting bianry string
BinaryUtils.multiply(a: string, b: string): string
: Multiplies two binary strings and returns the resulting bianry string
(NOT WORKING) BinaryUtils.divide(a: string, b: string): string
: Divides two binary strings and returns the resulting bianry string
BinaryUtils.power(base: string, exponent: string): string
: Raises a binary string base
to the power of exponent
and returns resulting binary string.
Examples
Converting a number to binary and performing an addition on it.
const a = BinaryUtils.toBinary(1234);
const b = BinaryUtils.toBinary(1234);
const result = BinaryUtils.add(a, b); // Returns: 2468
Simple conversion of text to binary
const bin = BinaryUtils.stringToBinary('hello world');
String math
const strA = BinaryUtils.stringToBinary('hello world');
const strB = BinaryUtils.stringToBinary('goodbye world');
const result = BinaryUtils.add(strA, strB); // Returns: result of addition between binary 'hello world' and binary 'goodbye world'
Reversing string math
const strA = BinaryUtils.stringToBinary('hello world');
const strB = BinaryUtils.stringToBinary('goodbye world');
const added = BinaryUtils.add(strA, strB); // Returns: result of addition between binary 'hello world' and binary 'goodbye world'
const result = BinaryUtils.subtract(added, strB); // Returns: 'hello world'
Contributing
Fork and make a Pull Request with your code. No guarantee it'll be accepted.
Known Caveats
1.0.x
:
BinaryUtils.divide
does not work. I don't know how to implement that logic as of yet.
Planned Features
1.1.0
: Add tests for and fix the non-working BinaryUtils.divide
method. Could be pushed to 2.0.0; Add typescript definitions.
2.0.0
: Rework the whole thing to work with indefinitely-sized buffers to perform operations instead of raw strings. No guarantees on a 2.0.x release.