Skip to content
This repository was archived by the owner on Feb 6, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/org/abstractj/kalium/NaCl.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ int crypto_aead_chacha20poly1305_decrypt(
@Deprecated
int BOXZERO_BYTES = 16;

int CRYPTO_BOX_CURVE25519XSALSA20POLY1305_SEEDBYTES = 32;

int CRYPTO_BOX_CURVE25519XSALSA20POLY1305_PUBLICKEYBYTES = 32;

int CRYPTO_BOX_CURVE25519XSALSA20POLY1305_SECRETKEYBYTES = 32;
Expand All @@ -217,7 +219,10 @@ int crypto_aead_chacha20poly1305_decrypt(
int CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BEFORENMBYTES = 32;

int crypto_scalarmult_base(
@Out byte[] publicKey, @In byte[] secretKey);
@Out byte[] publicKey, @In byte[] secretKey);

int crypto_box_curve25519xsalsa20poly1305_seed_keypair(
@Out byte[] publicKey, @Out byte[] secretKey, @In byte[] seed);

int crypto_box_curve25519xsalsa20poly1305_keypair(
@Out byte[] publicKey, @Out byte[] secretKey);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/abstractj/kalium/keys/KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public KeyPair(String secretKey, Encoder encoder) {
this(encoder.decode(secretKey));
}

private KeyPair(byte[] secretKey, byte[] publicKey) {
this.secretKey = secretKey;
this.publicKey = publicKey;
}

public static KeyPair seeded(byte[] seed) {
byte[] secretKey = zeros(CRYPTO_BOX_CURVE25519XSALSA20POLY1305_SECRETKEYBYTES);
byte[] publicKey = zeros(CRYPTO_BOX_CURVE25519XSALSA20POLY1305_PUBLICKEYBYTES);
sodium().crypto_box_curve25519xsalsa20poly1305_seed_keypair(publicKey, secretKey, seed);
return new KeyPair(secretKey, publicKey);
}

public PublicKey getPublicKey() {
return new PublicKey(publicKey);
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/abstractj/kalium/keys/KeyPairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.abstractj.kalium.keys;

import org.abstractj.kalium.*;
import org.abstractj.kalium.crypto.*;
import org.junit.Ignore;
import org.junit.Test;

Expand All @@ -41,6 +43,18 @@ public void testGenerateKeyPair() {
}
}

@Test
public void testGenerateSeededKeyPair() {
try {
byte[] seed = new Random().randomBytes(NaCl.Sodium.CRYPTO_BOX_CURVE25519XSALSA20POLY1305_SEEDBYTES);
KeyPair key = KeyPair.seeded(seed);
assertTrue(key.getPrivateKey() != null);
assertTrue(key.getPublicKey() != null);
} catch (Exception e) {
fail("Should return a valid key size");
}
}

@Test
public void testAcceptsValidKey() {
try {
Expand Down