From 6219288a0a9eea584f66cd4f1c81f62e1dc692f7 Mon Sep 17 00:00:00 2001 From: Nemo <me@captnemo.in> Date: Sat, 06 Jan 2024 22:18:35 +0530 Subject: [PATCH] WIP --- .gitignore | 3 ++- Package.resolved | 14 ++++++++++++++ Package.swift | 27 +++++++++++++++++++++++++++ Sources/EpicQR/EpicQR.swift | 41 +++++++++++++++++++++++++++++++++++++++++ Tests/EpicQRTests/EpicQRTests.swift | 10 ++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 25a4539..2a0268f 100644 --- a/.gitignore +++ a/.gitignore @@ -1,5 +1,6 @@ haxe-build.zip build/ *.jar -*.class+*.class +.builddiff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..fe33793 100644 --- /dev/null +++ a/Package.resolved @@ -1,0 +1,14 @@ +{ + "pins" : [ + { + "identity" : "swift-crypto", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-crypto.git", + "state" : { + "revision" : "b51f1d6845b353a2121de1c6a670738ec33561a6", + "version" : "3.1.0" + } + } + ], + "version" : 2 +} diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..dc80234 100644 --- /dev/null +++ a/Package.swift @@ -1,0 +1,27 @@ +// swift-tools-version: 5.9 + +import PackageDescription + +let package = Package( + name: "EpicQR", + products: [ + .library( + name: "EpicQR", + targets: ["EpicQR"]), + ], + dependencies: [ + .package(url: "https://github.com/apple/swift-crypto.git", from:"3.1.0") + ], + targets: [ + .target( + name: "EpicQR", + dependencies: [ + .product(name: "Crypto", package: "swift-crypto"), + .product(name: "_CryptoExtras", package: "swift-crypto"), + ] + ), + .testTarget( + name: "EpicQRTests", + dependencies: ["EpicQR"]), + ] +)diff --git a/Sources/EpicQR/EpicQR.swift b/Sources/EpicQR/EpicQR.swift new file mode 100644 index 0000000..ac65e10 100644 --- /dev/null +++ a/Sources/EpicQR/EpicQR.swift @@ -1,0 +1,41 @@ +import Foundation +import Crypto +import _CryptoExtras + +let IV : String = "H76$suq23_po(8sD"; +let KEY : String = "X_4k$uq23FSwI.qT"; + +struct EpicResult: Decodable { + let epic_no: String + let unique_generation_id: Int +} + +class EpicQR { + var epic : String + var id : Int + + init(qr: String){ + let jsonString = try! decode(qr).data(using: .utf8)! + let er:EpicResult = try! JSONDecoder().decode(EpicResult.self, from: jsonString) + self.epic = er.epic_no + self.id = er.unique_generation_id + } + + private func decode(_ qrString: String) throws -> String { + guard let decodedData = Data(base64Encoded: qrString) else { + throw DecodeError() + } + let ct = [UInt8](decodedData) + let iv = try AES._CBC.IV(ivBytes: [UInt8](IV.utf8)) + + let decodedText = try AES._CBC.decrypt(ct, + using: .init(data: [UInt8](KEY.utf8)), + iv: iv + ) + return String(decoding: decodedText, as: UTF8.self) + } +} + +private struct DecodeError: Error { + +}diff --git a/Tests/EpicQRTests/EpicQRTests.swift b/Tests/EpicQRTests/EpicQRTests.swift new file mode 100644 index 0000000..7a2d791 100644 --- /dev/null +++ a/Tests/EpicQRTests/EpicQRTests.swift @@ -1,0 +1,10 @@ +import XCTest +@testable import EpicQR + +final class EpicQRTests: XCTestCase { + func testExample() throws { + let result = try EpicQR.decode("dbhvecY6Roa4NF3gAzEbkTibZZzXAEYpMg8197BQWMS2+ID24FGDKWB5IEcuxjsA81ChprhSO3EsjKMRDbBWLg==") + XCTAssertEqual(result.epic, "ABC1234566") + XCTAssertEqual(result.id, 1234) + } +} -- rgit 0.1.5