Code Monkey home page Code Monkey logo

base58's Introduction

Maven Central

Install

Using:

repositories {
    mavenCentral()
}

Add dependency:

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

Usage

From simplest to most advanced:

Encode (static method)

String base58 = Base58.base58Encode(bytes);

Decode (static method)

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)

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

Decode (instance method)

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

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

Encode (to a target, instance method)

Either:

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

Or let it get told the correct initial maximum size:

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

Or supply an implementation of EncodeTargetFromCapacity:

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

Decode (to a target, instance method)

static class ByteArrayTarget implements DecodeTarget {
    private int idx = 0;
    byte[] bytes;

    @Override
    public DecodeWriter getWriterForLength(int len) {
        bytes = new byte[len];
        return b -> bytes[idx++] = b;
    }
}

ByteArrayTarget target = new ByteArrayTarget();
Base58.newSecureInstance().decode(base58, target);
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.

base58's People

Contributors

westonal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.