This repository was archived by the owner on Mar 10, 2025. It is now read-only.
forked from terl/lazysodium-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
sean-codevasp edited this page Sep 3, 2024
·
5 revisions
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.
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'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);
}
}