Using:
repositories {
mavenCentral()
}Add dependency:
dependencies {
implementation 'io.github.novacrypto:Base58:2022.01.17@jar'
}From simplest to most advanced:
Stringbase58 = Base58.base58Encode(bytes);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.
Stringbase58 = Base58.newInstance().encode(bytes);byte[] bytes = Base58.newInstance().decode(base58CharSequence);The instances are not threadsafe, never share an instance across threads.
Either:
finalStringBuildersb = newStringBuilder();
Base58.newSecureInstance().encode(bytes, sb::append);
returnsb.toString();Or let it get told the correct initial maximum size:
finalStringBuildersb = newStringBuilder();
Base58.newSecureInstance().encode(bytes, sb::ensureCapacity, sb::append);
returnsb.toString();Or supply an implementation of EncodeTargetFromCapacity:
finalStringBuildersb = newStringBuilder();
Base58.newSecureInstance().encode(bytes, (charLength) -> {
// gives you a chance to allocate memory before passing the buffer as an EncodeTargetsb.ensureCapacity(charLength);
returnsb::append; // EncodeTarget
});
returnsb.toString();staticclassByteArrayTargetimplementsDecodeTarget {
privateintidx = 0;
byte[] bytes;
@OverridepublicDecodeWritergetWriterForLength(intlen) {
bytes = newbyte[len];
returnb -> bytes[idx++] = b;
}
}
ByteArrayTargettarget = newByteArrayTarget();
Base58.newSecureInstance().decode(base58, target);
target.bytes;These advanced usages avoid allocating memory and allow SecureByteBuffer usage.
- Update dependencies
- Add
EncodeTargetFromCapacityandEncodeTargetCapacityinterfaces and relatedSecureEncoder#encodemethod overloads
- uses static
SecureRandomon the advice of Spotbugs, and while it was a false positive intended forRandomuse warning, it's not a bad thing to do anyway.