项目作者: NovaCrypto

项目描述 :
Base58 Encode Decode
高级语言: Java
项目地址: git://github.com/NovaCrypto/Base58.git
创建时间: 2017-10-14T19:03:42Z
项目社区:https://github.com/NovaCrypto/Base58

开源协议:GNU General Public License v3.0

下载


Maven Central

Install

Using:

  1. repositories {
  2. mavenCentral()
  3. }

Add dependency:

  1. dependencies {
  2. implementation 'io.github.novacrypto:Base58:2022.01.17@jar'
  3. }

Usage

From simplest to most advanced:

Encode (static method)

  1. String base58 = Base58.base58Encode(bytes);

Decode (static method)

  1. byte[] bytes = Base58.base58Decode(base58String);

The static methods are threadsafe as they have a shared buffer per thread. They are named so they are still readable if you import static.

Encode (instance method)

  1. String base58 = Base58.newInstance().encode(bytes);

Decode (instance method)

  1. byte[] bytes = Base58.newInstance().decode(base58CharSequence);

The instances are not threadsafe, never share an instance across threads.

Encode (to a target, instance method)

Either:

  1. final StringBuilder sb = new StringBuilder();
  2. Base58.newSecureInstance().encode(bytes, sb::append);
  3. return sb.toString();

Or let it get told the correct initial maximum size:

  1. final StringBuilder sb = new StringBuilder();
  2. Base58.newSecureInstance().encode(bytes, sb::ensureCapacity, sb::append);
  3. return sb.toString();

Or supply an implementation of EncodeTargetFromCapacity:

  1. final StringBuilder sb = new StringBuilder();
  2. Base58.newSecureInstance().encode(bytes, (charLength) -> {
  3. // gives you a chance to allocate memory before passing the buffer as an EncodeTarget
  4. sb.ensureCapacity(charLength);
  5. return sb::append; // EncodeTarget
  6. });
  7. return sb.toString();

Decode (to a target, instance method)

  1. static class ByteArrayTarget implements DecodeTarget {
  2. private int idx = 0;
  3. byte[] bytes;
  4. @Override
  5. public DecodeWriter getWriterForLength(int len) {
  6. bytes = new byte[len];
  7. return b -> bytes[idx++] = b;
  8. }
  9. }
  10. ByteArrayTarget target = new ByteArrayTarget();
  11. Base58.newSecureInstance().decode(base58, target);
  12. target.bytes;

These advanced usages avoid allocating memory and allow SecureByteBuffer usage.

Change Log

0.1.3

  • Update dependencies
  • Add EncodeTargetFromCapacity and EncodeTargetCapacity interfaces and related SecureEncoder#encode method overloads

2022.01.17

  • uses static SecureRandom on the advice of Spotbugs, and while it was a false positive intended for Random use warning, it’s not a bad thing to do anyway.