zalopay-oss / blake3-jni

JVM bindings for BLAKE3 hash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blake3 JNI

Overview

  • C Bindings for the API of BLAKE3 cryptographic hash function in Java.

Usage

<dependency>
  <groupId>vn.zalopay.zas.common.crypto</groupId>
  <artifactId>blake3-jni</artifactId>
  <version>1.0.0</version>
</dependency>

API

The library has the same c api:

  // Verifies the library is connected
  public static boolean isEnabled() 
  
  // Creates a NativeBlake3 instance and a equivalent one as `blake3_hasher` in c.
  public NativeBlake3() throws IllegalStateException 
  
  // Initializers
  public void initDefault()
  public void initKeyed(byte[] key)
  public void initDeriveKey(String context)
  
  // Add input to the hasher. This can be called any number of times.
  public void update(byte[] data)
  
  // Equivalent to blake3_hasher_finalize in C. you can keep adding data after calling this. 
  public byte[] getOutput() throws InvalidNativeOutput 
  public byte[] getOutput(int outputLength) throws InvalidNativeOutput

Example

  • Note: Using in try-with-resources block to auto-close resource allocated in memory in C.
  // Initialize the hasher
  try (NativeBlake3 hasher = new NativeBlake3()) {
      hasher.initDefault();
  
      // read data
      byte[] data = "example data".getBytes();
      hasher.update(data);
      
      // more data
      byte[] moredata = "more data".getBytes();
      hasher.update(moredata);
  
      // Finalize the hash. BLAKE3 output length defaults to 32 bytes
      byte[] output = hasher.getOutput();
  }

Build from scratch

  • Currently, this library is build for Linux and Mac OS (M1). detect the OS and load the correct binary.

  • Linux's library is integrated build with Gitlab Runner pipeline. For MacOS build, follow the following guides.

  • For more environment, see here.

Mac OS M1

  • Requirements:

    • Java
    • Maven
    • GCC
  • Run the command to build native shared library.

$ ./scripts/make.sh

# Output will be native library binaries in `resources` folder.
# libblake3.sylib
  • Build jar with Maven.
$ ./scripts/build.sh

About

JVM bindings for BLAKE3 hash

License:Apache License 2.0


Languages

Language:Assembly 62.6%Language:Rust 23.6%Language:C 12.5%Language:Java 0.6%Language:Python 0.5%Language:Shell 0.2%