-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d97f715
commit 1ae2ccd
Showing
7 changed files
with
178 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Data+Base64URL.swift | ||
// MinterWallet | ||
// | ||
// Created by Alexey Sidorov on 11.03.2020. | ||
// Copyright © 2020 Minter. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Extension for making base64 representations of `Data` safe for | ||
/// transmitting via URL query parameters | ||
extension Data { | ||
|
||
/// Instantiates data by decoding a base64url string into base64 | ||
/// | ||
/// - Parameter string: A base64url encoded string | ||
init?(base64URLEncoded string: String) { | ||
self.init(base64Encoded: string.toggleBase64URLSafe(on: false)) | ||
} | ||
|
||
/// Encodes the string into a base64url safe representation | ||
/// | ||
/// - Returns: A string that is base64 encoded but made safe for passing | ||
/// in as a query parameter into a URL string | ||
func base64URLEncodedString() -> String { | ||
return self.base64EncodedString().toggleBase64URLSafe(on: true) | ||
} | ||
|
||
} | ||
|
||
extension String { | ||
|
||
/// Encodes or decodes into a base64url safe representation | ||
/// | ||
/// - Parameter on: Whether or not the string should be made safe for URL strings | ||
/// - Returns: if `on`, then a base64url string; if `off` then a base64 string | ||
func toggleBase64URLSafe(on: Bool) -> String { | ||
if on { | ||
// Make base64 string safe for passing into URL query params | ||
let base64url = self.replacingOccurrences(of: "/", with: "_") | ||
.replacingOccurrences(of: "+", with: "-") | ||
.replacingOccurrences(of: "=", with: "") | ||
return base64url | ||
} else { | ||
// Return to base64 encoding | ||
var base64 = self.replacingOccurrences(of: "_", with: "/") | ||
.replacingOccurrences(of: "-", with: "+") | ||
// Add any necessary padding with `=` | ||
if base64.count % 4 != 0 { | ||
base64.append(String(repeating: "=", count: 4 - base64.count % 4)) | ||
} | ||
return base64 | ||
} | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.