-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #738 from seadowg/extract-signed
Add `extract-signed`
- Loading branch information
Showing
7 changed files
with
356 additions
and
79 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
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
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,88 @@ | ||
package org.javarosa.core.util; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
// Copied from: https://github.com/brsanthu/migbase64/blob/master/src/main/java/com/migcomponents/migbase64/Base64.java | ||
// Irrelevant after Java8 | ||
public class Base64 { | ||
|
||
private static final char[] BASE_64_TBL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); | ||
private static final int[] IA = new int[256]; | ||
static { | ||
Arrays.fill(IA, -1); | ||
for (int i = 0, iS = BASE_64_TBL.length; i < iS; i++) | ||
IA[BASE_64_TBL[i]] = i; | ||
IA['='] = 0; | ||
} | ||
|
||
public static String encode(byte[] sArr) { | ||
int sLen = sArr.length; | ||
int sOff = 0; | ||
|
||
if (sLen == 0) | ||
return ""; | ||
|
||
int eLen = (sLen / 3) * 3; | ||
int dLen = ((sLen - 1) / 3 + 1) << 2; | ||
byte[] dArr = new byte[dLen]; | ||
|
||
for (int s = sOff, d = 0; s < sOff + eLen; ) { | ||
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff); | ||
dArr[d++] = (byte) BASE_64_TBL[(i >>> 18) & 0x3f]; | ||
dArr[d++] = (byte) BASE_64_TBL[(i >>> 12) & 0x3f]; | ||
dArr[d++] = (byte) BASE_64_TBL[(i >>> 6) & 0x3f]; | ||
dArr[d++] = (byte) BASE_64_TBL[i & 0x3f]; | ||
} | ||
|
||
int left = sLen - eLen; | ||
if (left > 0) { | ||
int i = ((sArr[sOff + eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sOff + sLen - 1] & 0xff) << 2) : 0); | ||
dArr[dLen - 4] = (byte) BASE_64_TBL[i >> 12]; | ||
dArr[dLen - 3] = (byte) BASE_64_TBL[(i >>> 6) & 0x3f]; | ||
dArr[dLen - 2] = left == 2 ? (byte) BASE_64_TBL[i & 0x3f] : (byte) '='; | ||
dArr[dLen - 1] = '='; | ||
} | ||
|
||
return new String(dArr, StandardCharsets.UTF_8); | ||
} | ||
|
||
public static byte[] decode(byte[] sArr) { | ||
int sepCnt = 0; | ||
for (byte b : sArr) | ||
if (IA[b & 0xff] < 0) | ||
sepCnt++; | ||
|
||
if ((sArr.length - sepCnt) % 4 != 0) | ||
return new byte[0]; | ||
|
||
int pad = 0; | ||
for (int i = sArr.length; i > 1 && IA[sArr[--i] & 0xff] <= 0; ) | ||
if (sArr[i] == '=') | ||
pad++; | ||
|
||
int len = ((sArr.length - sepCnt) * 6 >> 3) - pad; | ||
|
||
byte[] dArr = new byte[len]; | ||
|
||
for (int s = 0, d = 0; d < len; ) { | ||
int i = 0; | ||
for (int j = 0; j < 4; j++) { | ||
int c = IA[sArr[s++] & 0xff]; | ||
if (c >= 0) | ||
i |= c << (18 - j * 6); | ||
else | ||
j--; | ||
} | ||
|
||
dArr[d++] = (byte) (i >> 16); | ||
if (d < len) { | ||
dArr[d++] = (byte) (i >> 8); | ||
if (d < len) | ||
dArr[d++] = (byte) i; | ||
} | ||
} | ||
|
||
return dArr; | ||
} | ||
} |
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,50 @@ | ||
package org.javarosa.core.util; | ||
|
||
import org.bouncycastle.crypto.Signer; | ||
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; | ||
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; | ||
|
||
@Nullable | ||
public static String extractSigned(byte[] contents, byte[] publicKey) { | ||
if (contents.length < 64) { | ||
return null; | ||
} | ||
|
||
byte[] signature = new byte[SIGNATURE_LENGTH]; | ||
System.arraycopy(contents, 0, signature, 0, SIGNATURE_LENGTH); | ||
|
||
int messageLength = contents.length - SIGNATURE_LENGTH; | ||
byte[] message = new byte[messageLength]; | ||
System.arraycopy(contents, SIGNATURE_LENGTH, message, 0, messageLength); | ||
|
||
if (verify(publicKey, signature, message)) { | ||
return new String(message, StandardCharsets.UTF_8); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private static boolean verify(byte[] publicKey, byte[] signature, byte[] message) { | ||
try { | ||
Ed25519PublicKeyParameters publicKeyParameters = new Ed25519PublicKeyParameters(publicKey, 0); | ||
Signer signer = new Ed25519Signer(); | ||
signer.init(false, publicKeyParameters); | ||
signer.update(message, 0, message.length); | ||
|
||
return signer.verifySignature(signature); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
// The key was too small | ||
return false; | ||
} catch (IllegalArgumentException e) { | ||
// The key was invalid | ||
return false; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.