https://github.com/novacrypto/base58
Base58 Encode Decode
https://github.com/novacrypto/base58
base58 bitcoin litecoin
Last synced: about 2 months ago
JSON representation
Base58 Encode Decode
- Host: GitHub
- URL: https://github.com/novacrypto/base58
- Owner: NovaCrypto
- License: gpl-3.0
- Created: 2017-10-14T19:03:42.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2022-01-19T00:29:47.000Z (over 3 years ago)
- Last Synced: 2023-07-28T10:10:22.399Z (about 2 years ago)
- Topics: base58, bitcoin, litecoin
- Language: Java
- Homepage: https://novacrypto.github.io/Base58
- Size: 659 KB
- Stars: 13
- Watchers: 3
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://search.maven.org/artifact/io.github.novacrypto/Base58/)
# Install
Using:
```groovy
repositories {
mavenCentral()
}
```Add dependency:
```groovy
dependencies {
implementation 'io.github.novacrypto:Base58:2022.01.17@jar'
}
```# Usage
From simplest to most advanced:
## Encode (static method)
```java
String base58 = Base58.base58Encode(bytes);
```## Decode (static method)
```java
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)
```java
String base58 = Base58.newInstance().encode(bytes);
```## Decode (instance method)
```java
byte[] bytes = Base58.newInstance().decode(base58CharSequence);
```The instances are not threadsafe, never share an instance across threads.
## Encode (to a target, instance method)
Either:
```java
final StringBuilder sb = new StringBuilder();
Base58.newSecureInstance().encode(bytes, sb::append);
return sb.toString();
```Or let it get told the correct initial maximum size:
```java
final StringBuilder sb = new StringBuilder();
Base58.newSecureInstance().encode(bytes, sb::ensureCapacity, sb::append);
return sb.toString();
```Or supply an implementation of `EncodeTargetFromCapacity`:
```java
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)
```java
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](https://github.com/NovaCrypto/SecureString/blob/master/src/main/java/io/github/novacrypto/SecureByteBuffer.java) 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.