-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Diffie Hellman, X25519, ECDH over Curve25519
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
app/src/androidTest/java/com/goterl/lazycode/lazysodium/DiffieHellmanAndroidTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
} |
Submodule lazysodium-java
updated
6 files