Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怒求Swift 版 #2

Open
wxlpp opened this issue Jul 31, 2018 · 1 comment
Open

怒求Swift 版 #2

wxlpp opened this issue Jul 31, 2018 · 1 comment

Comments

@wxlpp
Copy link

wxlpp commented Jul 31, 2018

No description provided.

@wxlpp
Copy link
Author

wxlpp commented Jul 31, 2018

这样为何不行求解

extension String {
    func tlsBase64URLDecode() -> String {
      let str = replacingOccurrences(of: "_", with: "=")
                .replacingOccurrences(of: "-", with: "/")
                .replacingOccurrences(of: "*", with: "+")
        guard let data = Data(base64Encoded: str),
              let decodeStr = String(data: data, encoding: .utf8) else {
            return str
        }
        return decodeStr
    }

    func tlsBase64URLDecode() -> String {
        guard let urlEncodeData = self.data(using: .utf8) else { return self }
        var bytes = [UInt8](urlEncodeData)
        for (index,byte) in bytes.enumerated() {
            if byte == 0x5f {
                bytes[index] = 0x3d
            }else if byte == 0x2d {
                bytes[index] = 0x2f
            }else if byte == 0x2a {
                bytes[index] = 0x2b
            }
        }
        print(String(bytes: bytes, encoding: .utf8))
        if let decodeData = Data(base64Encoded: Data(bytes: bytes)),
            let decodeStr = String(data: decodeData, encoding: .utf8) {
            return decodeStr
        }
        return self
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant