ios - 在 Swift 上解码 JSON 时解密字符串

标签 ios json swift encryption decode

我有一个 JSON 文件和一个相关类。我通过调用 init?(data: Data) 初始值设定项对其进行解码。我正在从端点获取此 JSON。我想用 SHA-256 加密 URL 文本。我的问题是我想在解码数据时解密这个 URL 字符串。所以在解码URL的时候,需要调用一个函数。有什么可行的方法吗?

PS:我知道我可以编写加密文本并在我将使用的地方解密它,但我想把它作为最后的选择。

struct TableCellData: Codable {
    let type: Int
    let cellText: String
    let cellImage: String?
    let url: URL?
    let detailText: String?
    let tableID: Int?

    enum CodingKeys: String, CodingKey {
        case type = "type"
        case cellText = "cell_text"
        case cellImage = "cell_image"
        case url = "url"
        case detailText = "detail_text"
        case tableID = "table_id"
    }
}

extension TableCellData {
    init?(data: Data) {
        guard let me = try? JSONDecoder().decode(TableCellData.self, from: data) else { return nil }
        self = me
    }

    init?(_ json: String, using encoding: String.Encoding = .utf8) {
        guard let data = json.data(using: encoding) else { return nil }
        self.init(data: data)
    }

    init?(fromURL url: String) {
        guard let url = URL(string: url) else { return nil }
        guard let data = try? Data(contentsOf: url) else { return nil }
        self.init(data: data)
    }

    var jsonData: Data? {
        return try? JSONEncoder().encode(self)
    }

    var json: String? {
        guard let data = self.jsonData else { return nil }
        return String(data: data, encoding: .utf8)
    }
}

最佳答案

如果我理解正确的话,您有一个字段 URL,您将在服务器端对其进行 SHA256 加密。 然后您将收到在 json 中加密的它,并且您希望在类实例中对其进行解密。

如果是这样,只需查看文档:encoding and decoding custom types并搜索标题:手动编码和解码

第一段代码是结构,第二段是自定义解析器,您可以在其中对您的字段进行 sha256 加密。

编辑:

不幸的是,我没有时间为您编写代码,但也许这个关于可编码和编码 key 的更深入的教程会对您有所帮助(寻找标题“手动编码和解码”):Tutorial Swift 4.0 Encodable

要点非常简单:您在可解码扩展中提供自己的解码逻辑。在这里,他们将宽度和高度分组在一个 Size 变量中:

  struct Photo
{
    var title: String
    var size: Size

    enum CodingKeys: String, CodingKey
    {
        case title = "name"
        case width
        case height
    }
}

extension Photo: Encodable
{
    func encode(to encoder: Encoder) throws
    {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(title, forKey: .title)
        try container.encode(size.width, forKey: .width)
        try container.encode(size.height, forKey: .height)
    }
}

extension Photo: Decodable
{
    init(from decoder: Decoder) throws
    {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        title = try values.decode(String.self, forKey: .title)
        let width = try values.decode(Double.self, forKey: .width)
        let height = try values.decode(Double.self, forKey: .height)
        size = Size(width: width, height: height)
    }
}

关于ios - 在 Swift 上解码 JSON 时解密字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48500744/

相关文章:

ios - 战舰游戏IOS概念设计

iphone - 如何在第一个图像启动时删除状态栏

C#将json序列化为class

ios - Box2D:如何使用 b2ChainShape 制作带有正方形的基于图 block 的 map

ios - 如何忽略容器的安全区域并尊重 Swift UI 中内容的安全区域

android - 如何获取OpenGL在移动设备中使用的内存总量?

ios - 使用 Swift 时出错 - 实例成员不能用于类型 'ViewController'

java - Jackson JSON 使用注释进行 pretty-print

javascript - 将 json 响应保存为常量

ios - 升级到 Swift 1.2 后 SpriteKit 出现问题