Skip to content

Commit

Permalink
Added Diffie Hellman, X25519, ECDH over Curve25519
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreet- committed Jul 31, 2018
1 parent 95b51cd commit 88764da
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {
ext {
artifactId = "lazysodium-android"
groupId = "com.goterl.lazycode"
version = '3.2.0'
version = '3.3.0'
description = "Lazysodium (Android) makes it effortless for Android " +
"developers to get started with Libsodium's cryptography."
androidTestDir = "src/androidTest/java/com/goterl/lazycode/lazysodium"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) Terl Tech Ltd • 31/07/18 18:54 • goterl.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
*/

/*
* This Java source file was generated by the Gradle 'init' task.
*/
import com.goterl.lazycode.lazysodium.exceptions.SodiumException;
import com.goterl.lazycode.lazysodium.interfaces.Box;
import com.goterl.lazycode.lazysodium.interfaces.DiffieHellman;
import com.goterl.lazycode.lazysodium.interfaces.SecretBox;
import com.goterl.lazycode.lazysodium.interfaces.StreamJava;
import com.goterl.lazycode.lazysodium.utils.Key;
import junit.framework.TestCase;
import org.junit.Test;

public class DiffieHellmanAndroidTest extends BaseTest {

private String clientSecretKey = "CLIENT_TOP_SECRET_KEY_1234567890";
private String serverSecretKey = "SERVER_TOP_SECRET_KEY_1234567890";


@Test
public void create() throws SodiumException {
DiffieHellman.Lazy dh = (DiffieHellman.Lazy) lazySodium;
SecretBox.Lazy box = (SecretBox.Lazy) lazySodium;

Key secretKeyC = Key.fromPlainString(clientSecretKey);
Key publicKeyC = dh.cryptoScalarMultBase(secretKeyC);

Key secretKeyS = Key.fromPlainString(serverSecretKey);
Key publicKeyS = dh.cryptoScalarMultBase(secretKeyS);

// -----
// ON THE CLIENT
// -----

// Compute a shared key for sending from client
// to server.
Key sharedKey = dh.cryptoScalarMult(secretKeyC, publicKeyS);

String message = "Hello";
byte[] nonce = new byte[Box.NONCEBYTES];
String encrypted = box.cryptoSecretBoxEasy(message, nonce, sharedKey);

// Send 'encrypted' to server...


// -----
// ON THE SERVER
// -----

// Compute the shared key for receiving server messages from client
Key sharedKeyServer = dh.cryptoScalarMult(secretKeyS, publicKeyC);
String decrypted = box.cryptoSecretBoxOpenEasy(encrypted, nonce, sharedKeyServer);

// 'decrypted' == Hello

TestCase.assertEquals(message, decrypted);
}



}

0 comments on commit 88764da

Please sign in to comment.