Skip to content

Commit

Permalink
Add test for unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Dec 12, 2023
1 parent 26a2cb3 commit 92d923c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/org/javarosa/core/util/Ed25519.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bouncycastle.crypto.signers.Ed25519Signer;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;

public class Ed25519 {

private static final int SIGNATURE_LENGTH = 64;
Expand All @@ -23,7 +25,7 @@ public static String extractSigned(byte[] contents, byte[] publicKey) {
System.arraycopy(contents, SIGNATURE_LENGTH, message, 0, messageLength);

if (verify(publicKey, signature, message)) {
return new String(message);
return new String(message, StandardCharsets.UTF_8);
} else {
return null;
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/javarosa/xpath/expr/ExtractSignedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ public void whenPublicKeyIsInvalid_returnsEmptyString() throws Exception {
assertThat(scenario.answerOf("/data/extracted"), is(nullValue()));
}

@Test
public void whenNonSignaturePartIncludesUnicode_successfullyDecodes() throws Exception{
String message = "🎃";
AsymmetricCipherKeyPair keyPair = createKeyPair();

byte[] signedMessage = signMessage(message, keyPair.getPrivate());
String encodedPublicKey = Encoding.BASE64.encode(((Ed25519PublicKeyParameters) keyPair.getPublic()).getEncoded());
String encodedContents = Encoding.BASE64.encode(signedMessage);

Scenario scenario = createScenario(encodedContents, encodedPublicKey);
assertThat(scenario.answerOf("/data/extracted"), is(stringAnswer(message)));
}

private static AsymmetricCipherKeyPair createKeyPair() {
Ed25519KeyPairGenerator ed25519KeyPairGenerator = new Ed25519KeyPairGenerator();
ed25519KeyPairGenerator.init(new Ed25519KeyGenerationParameters(new SecureRandom()));
Expand Down

0 comments on commit 92d923c

Please sign in to comment.