base58Encode method

String base58Encode(
  1. String hexNumber,
  2. BigInt alphabetNumber
)

Base58 encodes a hexadecimal number using the specified alphabet.

Implementation

String base58Encode(String hexNumber, BigInt alphabetNumber) {
  BigInt decimalValue = _hexToDecimal(hexNumber);
  String result = '';
  String alphabetUsed;

  if (alphabetNumber == BigInt.from(36)) {
    alphabetUsed = NosoConst.b36Alphabet;
  } else {
    alphabetUsed = NosoConst.b58Alphabet;
  }

  while (decimalValue.bitLength >= 2) {
    _DivResult divResult = _divideBigInt(decimalValue, alphabetNumber);
    decimalValue = divResult.coefficient;
    int remainder = divResult.remainder.toInt();
    result = alphabetUsed[remainder] + result;
  }

  if (decimalValue >= alphabetNumber) {
    _DivResult divResult = _divideBigInt(decimalValue, alphabetNumber);
    decimalValue = divResult.coefficient;
    int remainder = divResult.remainder.toInt();
    result = alphabetUsed[remainder] + result;
  }

  if (decimalValue > BigInt.zero) {
    int value = decimalValue.toInt();
    result = alphabetUsed[value] + result;
  }

  return result;
}