Skip to content

Commit

Permalink
AESDecoder 增加解密字串陣列的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Darktt committed Jan 12, 2024
1 parent 04e9b77 commit b6011ed
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 11 deletions.
31 changes: 31 additions & 0 deletions JsonDecodeProtectionTests/AESDecoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ struct AESObject: Decodable
var url: String?
}

struct AESObject2: Decodable
{
@AESDecoder(adopter: AESObject.AESAdopting.self)
private(set)
var urls: Array<String>?
}

extension AESObject
{
struct AESAdopting: AESAdopter
Expand Down Expand Up @@ -72,4 +79,28 @@ final class AESDecoderTest: XCTestCase

XCTAssertEqual(url, expect)
}

func testDecoderStringArraySuccess() throws
{
// Arrange
self.jsonString = """
{
"urls": [
"0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw=",
"0NhMzVQIsjShyNnck3huFVjVCcku2a+iAQVfY3CDrUw="
]
}
"""
let jsonData: Data = self.jsonString.data(using: .utf8)!
let jsonDecoder = JSONDecoder()

// Act
let object = try jsonDecoder.decode(AESObject2.self, from: jsonData)

// Assert
let urls: Array<String>? = object.urls
let expect: Array<String> = ["https://www.apple.com", "https://www.apple.com"]

XCTAssertEqual(urls, expect)
}
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

* File > Swift Packages > Add Package Dependency
* Add https://github.com/Darktt/JsonProtection
* Select "Up to Next Major" with "1.0.5"
* Select "Up to Next Major" with "1.0.6"

## 功能說明

Expand Down Expand Up @@ -260,6 +260,10 @@ struct AESObject: Decodable
}
```

> 支援型態:
> * 字串
> * 字串陣列
---
## MultipleKeysProtection (未完成)
保護同變數在不同 json 資料情境下,有複數的 key 存在的問題
66 changes: 56 additions & 10 deletions SourceCode/JsonProtection/AESDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ protocol AESAdopter
static var options: DTAES.Options { get }
}

// MARK: - AESDecodable -

public
protocol AESDecodable: Decodable { }

extension String: AESDecodable { }

extension Array: AESDecodable where Element == String { }

// MARK: - AESDecoder -

@propertyWrapper
public
struct AESDecoder<Adopter> where Adopter: AESAdopter
struct AESDecoder<Adopter, Value> where Adopter: AESAdopter, Value: AESDecodable
{
// MARK: - Properties -

public
var wrappedValue: String?
var wrappedValue: Value?

// MARK: - Methods -
// MARK: Initial Method
Expand All @@ -37,16 +46,14 @@ struct AESDecoder<Adopter> where Adopter: AESAdopter
init(adopter: Adopter.Type) { }
}

// MARK: - Conform Protocols -
// MARK: - Private Methods -

extension AESDecoder: Decodable
private
extension AESDecoder
{
public
init(from decoder: Decoder) throws
func decodeString(_ string: String) throws -> String
{
let container: SingleValueDecodingContainer = try decoder.singleValueContainer()
let encryptedString: String = try container.decode(String.self)
let aes = DTAES(encryptedString)
let aes = DTAES(string)
aes.setKey(Adopter.key)
Adopter.iv.map {

Expand All @@ -57,6 +64,45 @@ extension AESDecoder: Decodable

let decryptedString: String = try aes.result()

self.wrappedValue = decryptedString
return decryptedString
}

func decodeArray(_ array: Array<String>) throws -> Array<String>
{
let decryptedArray: Array<String> = try array.map {

try self.decodeString($0)
}

return decryptedArray
}
}


// MARK: - Conform Protocols -

extension AESDecoder: Decodable
{
public
init(from decoder: Decoder) throws
{
let container: SingleValueDecodingContainer = try decoder.singleValueContainer()
let encryptedValue: Value? = try? container.decode(Value.self)

if let encryptedString = encryptedValue as? String {

let decodeValue: String? = try? self.decodeString(encryptedString)
self.wrappedValue = decodeValue as? Value

return
}

if let encryptedArray = encryptedValue as? Array<String> {

let decodeValue: Array<String> = try self.decodeArray(encryptedArray)
self.wrappedValue = decodeValue as? Value

return
}
}
}

0 comments on commit b6011ed

Please sign in to comment.