Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.
sean-codevasp edited this page Sep 3, 2024 · 5 revisions

LazySodium Java CODE version - Usage Guide

Maven Central

Overview

lazysodium-java-code is a Java library that provides cryptographic functions based on the libsodium library. It aims to make cryptographic operations easier and more accessible for Java developers.

This guide will walk you through the steps to integrate and use the lazysodium-java-code library in your project.

Getting Started

1. Add Dependency

To use the lazysodium-java-code library in your Maven project, add the following dependency to your pom.xml file:

<dependency>
    <groupId>com.codevasp</groupId>
    <artifactId>lazysodium-java-code</artifactId>
    <version>1.0.1</version>
</dependency>

For Gradle users, add the following to your build.gradle file:

implementation 'com.codevasp:lazysodium-java-code:1.0.1'

2. Basic Usage

Once the dependency is added to your project, you can start using the cryptographic functions provided by the library.

Example: Generating a Key Pair

import com.codevasp.lazysodium.LazySodiumJava;
import com.codevasp.lazysodium.SodiumJava;
import com.codevasp.lazysodium.interfaces.Sign;

public class CryptoExample {
    public static void main(String[] args) {
        SodiumJava sodium = new SodiumJava();
        LazySodiumJava lazySodium = new LazySodiumJava(sodium);

        // Generate Private Key
        public static String generateSignKey() {
            try {
                KeyPair newKey = sodium.cryptoSignKeypair();
                byte[] key64 = newKey.getSecretKey().getAsBytes();
                byte[] key32 = new byte[32];
                System.arraycopy(key64, 0, key32, 0, 32);
                return Base64.getEncoder().encodeToString(key32);
            } catch (Exception e) {
                return null;
            }
        }

        // Generate Public Key
        public static String getPublicKey(String privateKey) throws SodiumException {
             byte[] seed = Base64.getDecoder().decode(privateKey);
             KeyPair signKeyPair = sodium.cryptoSignSeedKeypair(seed);
             return Base64.getEncoder().encodeToString(signKeyPair.getPublicKey().getAsBytes());
        }

        // Generate Key Pair
        String newKey = generateSignKey();
        log.info("new Private Key: {}", newKey);
        String newPubKey = getPublicKey(newKey);
        log.info("new Public Key: {}", newPubKey);
    }
}
Clone this wiki locally