Created
June 19, 2019 09:58
-
-
Save zonble/168dfaee9d4552850118580951000c70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CryptoKit | |
func jwt(header: String, payload: String, secret: String) -> String { | |
func base64URLencode(_ s: String) -> String { | |
return s.replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "+", with: "-") | |
} | |
let headerData = base64URLencode(header).data(using: .utf8)!.base64EncodedString() | |
let payloadData = base64URLencode(payload).data(using: .utf8)!.base64EncodedString() | |
let body = headerData + "." + payloadData | |
var hmac = HMAC<SHA256>(key: SymmetricKey(data: secret.data(using: String.Encoding.utf8)!)) | |
hmac.update(data: body.data(using: .utf8)!) | |
let signature = base64URLencode(Data(hmac.finalize()).base64EncodedString()) | |
return headerData + "." + payloadData + "." + signature | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment