Adds Java code

This commit is contained in:
Nemo 2024-01-06 13:16:34 +05:30
parent 6b429de1de
commit 90af000a33
11 changed files with 103 additions and 8 deletions

5
.gitignore vendored
View File

@ -1 +1,4 @@
library.zip
haxe-build.zip
build/
*.jar

View File

@ -69,7 +69,7 @@ Note the extra space.
##### Encoded Base64
```text
7+NGHfxDoqjXjk6iU0U6yTYhjWktxVe6eijj+9Nf36VYpzgIb+qIzxS6VSukYt6ANFCzuM6mZ4AO9oO4FFDAVw==
dbhvecY6Roa4NF3gAzEbkTibZZzXAEYpMg8197BQWMS2+ID24FGDKWB5IEcuxjsA81ChprhSO3EsjKMRDbBWLg==
```
See the recipe on [CyberChef][cyberchef].

View File

@ -1,4 +1,10 @@
#!/bin/sh
rm -f library.zip
zip -r library.zip src LICENSE *.md *.json *.hxml
haxelib submit library.zip $(pass show Dev/lib.haxe.org) --always
# Haxe
rm -f haxe-build.zip
zip -r haxe-build.zip src LICENSE *.md *.json *.hxml
haxelib submit haxe-build.zip $(pass show Dev/lib.haxe.org) --always
rm -f haxe-build.zip
# Java
javac -cp src src/eci/*.java -d build/
jar -cf epicqr-release.jar build/*

View File

@ -13,7 +13,7 @@ class EpicQR{
// public static inline var SALT:String = "FSwI.qT";
public static inline var IV:String = "H76$suq23_po(8sD";
public static inline var KEY:String = "X_4k$uq23FSwI.qT"; // KEY+SALT
public static inline var KEY:String = "X_4k$uq23FSwI.qT"; // KEY_PREFIX + SALT
static function decode(input:String){
var cipherText:Bytes = Base64.decode(input);

54
src/eci/EpicQR.java Normal file
View File

@ -0,0 +1,54 @@
package eci;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.InvalidAlgorithmParameterException;
import javax.crypto.NoSuchPaddingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EpicQR {
// static String KEY_SEED = "tHzHtCcDd3V6p_9dOnse|_SX_4k$uq23.qT.L.(MgyJ7UH4n921J6UlKeck_S0Jl2znUY8CiMKyklWf2";
// static String KEY_PREFIX = "X_4k$uq23"; // SUBSTR(KEY_SEED, 23, 32)
// static String SALT = "FSwI.qT";
static String IV = "H76$suq23_po(8sD";
static String KEY = "X_4k$uq23FSwI.qT"; // KEY_PREFIX + SALT
private static SecretKeySpec getKey() {
return new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
}
private static Cipher getCipher() throws NoSuchAlgorithmException, InvalidKeyException,NoSuchPaddingException,InvalidAlgorithmParameterException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getKey(), new IvParameterSpec(IV.getBytes()));
return cipher;
}
public static Result decode(String input) {
try {
byte[] decryptedBytes = getCipher().doFinal(Base64.getDecoder().decode(input));
String j = new String(decryptedBytes);
return extractValue(j, "epic_no", "unique_generated_id");
} catch (Exception exception) {
throw new RuntimeException("Failed to decode", exception);
}
}
private static Result extractValue(String jsonString, String k1, String k2) {
final String regex = "\\{\\\"epic_no\\\":\\\"([A-Z]{3}\\d{7})\\\",\\\"unique_generated_id\\\":(\\d+)\\}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(jsonString);
if (matcher.find()) {
return new Result(matcher.group(1), Integer.parseInt(matcher.group(2)));
} else {
throw new RuntimeException("Failed to parse JSON");
}
}
}

View File

@ -2,9 +2,9 @@ package eci;
class Result {
public var epic:String;
public var id:String;
public var id:Int;
public function new(epic: String, id: String) {
public function new(epic: String, id: Int) {
this.epic = epic;
this.id = id;
}

12
src/eci/Result.java Normal file
View File

@ -0,0 +1,12 @@
package eci;
public class Result{
public String epic;
public int id;
// constructor
public Result(String epic, int id){
this.epic = epic;
this.id = id;
}
}

5
test.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
## Tests for Java
javac -cp src/ test/Test.java
java -enableassertions -cp src:test Test

BIN
test/Test.class Normal file

Binary file not shown.

11
test/Test.java Normal file
View File

@ -0,0 +1,11 @@
import eci.Result;
import eci.EpicQR;
public class Test {
public static void main(String[] args) {
String e1 = "dbhvecY6Roa4NF3gAzEbkTibZZzXAEYpMg8197BQWMS2+ID24FGDKWB5IEcuxjsA81ChprhSO3EsjKMRDbBWLg==";
Result r1 = EpicQR.decode(e1);
assert r1.epic.equals("ABC1234566") : "Invalid EPIC";
assert r1.id == 1234 : "Invalid Unique ID";
}
}

4
test/fixture.json Normal file
View File

@ -0,0 +1,4 @@
{
"input": "{\"epic_no\":\"ABC1234566\",\"unique_generated_id\":1234}",
"output": "dbhvecY6Roa4NF3gAzEbkTibZZzXAEYpMg8197BQWMS2+ID24FGDKWB5IEcuxjsA81ChprhSO3EsjKMRDbBWLg=="
}