epicqr/src/eci/EpicQR.hx

59 lines
1.9 KiB
Haxe
Raw Normal View History

2024-01-05 17:26:40 +00:00
package eci;
#if php
import php.Global;
import php.Lib;
#elseif nodejs
import js.node.Crypto;
import js.node.Buffer;
#else
2024-01-05 16:22:42 +00:00
import haxe.io.Bytes;
import haxe.crypto.Base64;
2024-01-07 17:31:38 +00:00
import haxe.crypto.mode.CBC;
2024-01-05 16:22:42 +00:00
import haxe.Json;
#end
2024-01-05 16:22:42 +00:00
2024-01-07 17:31:38 +00:00
@:expose
2024-01-05 16:22:42 +00:00
class EpicQR{
// public static inline var KEY_SEED:String = "tHzHtCcDd3V6p_9dOnse|_SX_4k$uq23.qT.L.(MgyJ7UH4n921J6UlKeck_S0Jl2znUY8CiMKyklWf2";
// public static inline var KEY_PREFIX:String = "X_4k$uq23";
// public static inline var SALT:String = "FSwI.qT";
public static inline var IV:String = "H76$suq23_po(8sD";
2024-01-06 07:46:34 +00:00
public static inline var KEY:String = "X_4k$uq23FSwI.qT"; // KEY_PREFIX + SALT
#if nodejs
2024-01-07 17:31:38 +00:00
private static inline var UTF8:String = "utf8";
#end
2024-01-05 16:22:42 +00:00
2024-01-07 17:31:38 +00:00
static function decode(input : String){
#if nodejs
2024-01-07 17:37:04 +00:00
var c = Crypto.createDecipheriv("aes-128-cbc", Buffer.from(KEY, UTF8), Buffer.from(IV, UTF8));
2024-01-07 17:31:38 +00:00
c.setAutoPadding(false);
2024-01-07 17:37:04 +00:00
var decrypted = c.update(Buffer.from(input, "base64"));
2024-01-07 17:31:38 +00:00
var f = c.finalContents();
decrypted = Buffer.concat([decrypted, f]);
// Remove PKCS7 padding
var paddingLength = decrypted[decrypted.length - 1];
decrypted = decrypted.slice(0, decrypted.length - paddingLength);
2024-01-07 17:37:04 +00:00
var _d = haxe.Json.parse(decrypted.toString(UTF8));
#elseif php
var ct = Global.base64_decode(input);
var jsonString = Global.call_user_func('openssl_decrypt', ct, 'aes-128-cbc', KEY, 1, IV);
var _d = Global.json_decode(jsonString);
2024-01-07 17:31:38 +00:00
#else
2024-01-05 16:22:42 +00:00
var cipherText:Bytes = Base64.decode(input);
2024-01-07 17:31:38 +00:00
var c : Crypto = new Crypto();
2024-01-05 16:22:42 +00:00
var key = Bytes.ofString(KEY);
var iv = Bytes.ofString(IV);
2024-01-07 17:31:38 +00:00
c.init(key,iv);
var jsonString = c.decrypt(CBC,cipherText).toString();
2024-01-05 16:22:42 +00:00
var _d = Json.parse(jsonString);
2024-01-07 17:31:38 +00:00
#end
2024-01-07 17:37:04 +00:00
return new Result(_d.epic_no, _d.unique_generated_id);
2024-01-05 16:22:42 +00:00
}
}