Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different message encoding formats #70

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 61 additions & 51 deletions src/main/java/com/goterl/lazycode/lazysodium/LazySodium.java

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions src/main/java/com/goterl/lazycode/lazysodium/LazySodiumJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.goterl.lazycode.lazysodium.interfaces.Scrypt;
import com.goterl.lazycode.lazysodium.interfaces.StreamJava;
import com.goterl.lazycode.lazysodium.utils.Key;
import com.goterl.lazycode.lazysodium.utils.MessageEncoder;

import java.nio.charset.Charset;

Expand All @@ -31,6 +32,15 @@ public LazySodiumJava(SodiumJava sodium, Charset charset) {
this.sodium = sodium;
}

public LazySodiumJava(SodiumJava sodium, MessageEncoder messageEncoder) {
super(messageEncoder);
this.sodium = sodium;
}

public LazySodiumJava(SodiumJava sodium, Charset charset, MessageEncoder messageEncoder) {
super(charset, messageEncoder);
this.sodium = sodium;
}

@Override
public boolean cryptoPwHashScryptSalsa208Sha256(byte[] out, long outLen, byte[] password, long passwordLen, byte[] salt, long opsLimit, long memLimit) {
Expand Down Expand Up @@ -73,7 +83,7 @@ public String cryptoPwHashScryptSalsa208Sha256(String password, byte[] salt, lon
throw new SodiumException("Could not Scrypt hash your password.");
}

return toHex(hash);
return messageEncoder.encode(hash);
}

@Override
Expand All @@ -96,12 +106,12 @@ public String cryptoPwHashScryptSalsa208Sha256Str(String password, long opsLimit
throw new SodiumException("Could not string Scrypt hash your password.");
}

return toHex(hash);
return messageEncoder.encode(hash);
}

@Override
public boolean cryptoPwHashScryptSalsa208Sha256StrVerify(String hash, String password) {
byte[] hashBytes = toBin(hash);
byte[] hashBytes = messageEncoder.decode(hash);
byte[] passwordBytes = bytes(password);

// If the end of the hash does not have an null byte,
Expand Down Expand Up @@ -210,23 +220,23 @@ public byte[] cryptoStream(byte[] nonce, Key key, StreamJava.Method method) {
@Override
public String cryptoStreamXor(String message, byte[] nonce, Key key, StreamJava.Method method) {
byte[] mBytes = bytes(message);
return toHex(cryptoStreamDefaultXor(mBytes, nonce, key, method));
return messageEncoder.encode(cryptoStreamDefaultXor(mBytes, nonce, key, method));
}

@Override
public String cryptoStreamXorDecrypt(String cipher, byte[] nonce, Key key, StreamJava.Method method) {
return str(cryptoStreamDefaultXor(toBin(cipher), nonce, key, method));
return str(cryptoStreamDefaultXor(messageEncoder.decode(cipher), nonce, key, method));
}

@Override
public String cryptoStreamXorIc(String message, byte[] nonce, long ic, Key key, StreamJava.Method method) {
byte[] mBytes = bytes(message);
return toHex(cryptoStreamDefaultXorIc(mBytes, nonce, ic, key, method));
return messageEncoder.encode(cryptoStreamDefaultXorIc(mBytes, nonce, ic, key, method));
}

@Override
public String cryptoStreamXorIcDecrypt(String cipher, byte[] nonce, long ic, Key key, StreamJava.Method method) {
return str(cryptoStreamDefaultXorIc(toBin(cipher), nonce, ic, key, method));
return str(cryptoStreamDefaultXorIc(messageEncoder.decode(cipher), nonce, ic, key, method));
}

private byte[] cryptoStreamDefaultXor(byte[] messageBytes, byte[] nonce, Key key, StreamJava.Method method) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.goterl.lazycode.lazysodium.utils;

import java.util.Base64;

public class Base64MessageEncoder implements MessageEncoder {

@Override
public String encode(byte[] cipher) {
return Base64.getEncoder().encodeToString(cipher);
}

@Override
public byte[] decode(String cipherText) {
return Base64.getDecoder().decode(cipherText);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.goterl.lazycode.lazysodium.utils;

import com.goterl.lazycode.lazysodium.LazySodium;

public class HexMessageEncoder implements MessageEncoder {

@Override
public String encode(byte[] cipher) {
return LazySodium.toHex(cipher);
}

@Override
public byte[] decode(String cipherText) {
return LazySodium.toBin(cipherText);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/goterl/lazycode/lazysodium/utils/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.goterl.lazycode.lazysodium.LazySodium;

import java.nio.charset.Charset;
import java.util.Base64;

public class Key {
private byte[] key;
Expand Down Expand Up @@ -45,6 +46,15 @@ public static Key fromHexString(String hexString) {
return new Key(LazySodium.toBin(hexString));
}

/**
* Create a Key from a base64 string.
* @param base64String A base64 encoded string.
* @return A new Key.
*/
public static Key fromBase64String(String base64String) {
return new Key(Base64.getDecoder().decode(base64String));
}

/**
* Create a Key from a regular, unmodified, not encoded string.
* @param str A plain string.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.goterl.lazycode.lazysodium.utils;

public interface MessageEncoder {
String encode(byte[] cipher);
byte[] decode(String cipherText);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.goterl.lazycode.lazysodium.utils;

import com.goterl.lazycode.lazysodium.BaseTest;
import junit.framework.TestCase;
import org.junit.Test;

public class Base64MessageEncoderTest extends BaseTest {

@Test
public void decodeEqualsEncode() {
Base64MessageEncoder encoder = new Base64MessageEncoder();

String cipherText = "YS1iYXNlNjQtZW5jb2RlZC1zdHJpbmcK";
byte[] cipher = encoder.decode(cipherText);

TestCase.assertEquals(cipherText, encoder.encode(cipher));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.goterl.lazycode.lazysodium.utils;

import com.goterl.lazycode.lazysodium.BaseTest;
import junit.framework.TestCase;
import org.junit.Test;

public class HexMessageEncoderTest extends BaseTest {

@Test
public void decodeEqualsEncode() {
HexMessageEncoder encoder = new HexMessageEncoder();

String cipherText = "612D6865782D656E636F6465642D737472696E67";
byte[] cipher = encoder.decode(cipherText);

TestCase.assertEquals(cipherText, encoder.encode(cipher));
}
}