mirror of
https://github.com/bitwarden/ios.git
synced 2026-02-04 02:14:09 -06:00
Add base64url Data extensions
This commit is contained in:
parent
ec21ebb4c9
commit
3d80b2b824
@ -2,6 +2,28 @@ import CryptoKit
|
||||
import Foundation
|
||||
|
||||
public extension Data {
|
||||
// MARK: Initializers
|
||||
|
||||
/// Parses bytes from a base64url-encoded string.
|
||||
init?(base64UrlEncoded str: String) throws {
|
||||
// .ignoreUnknownCharacters allows unpadded strings as a side-effect.
|
||||
try self.init(base64Encoded: str.urlDecoded(), options: .ignoreUnknownCharacters)
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Encodes bytes in Data as a base64url string.
|
||||
func base64UrlEncodedString(trimPadding shouldTrim: Bool) -> String {
|
||||
let encoded = base64EncodedString()
|
||||
.replacingOccurrences(of: "+", with: "-")
|
||||
.replacingOccurrences(of: "/", with: "_")
|
||||
if shouldTrim {
|
||||
return encoded.trimmingCharacters(in: CharacterSet(["="]))
|
||||
} else {
|
||||
return encoded
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a hash value for the provided data.
|
||||
///
|
||||
/// - Parameter using: The type of cryptographically secure hashing being performed.
|
||||
|
||||
@ -13,4 +13,36 @@ class DataTests: BitwardenTestCase {
|
||||
"0101010101010101010101010101010101010101010101010101010101010101",
|
||||
)
|
||||
}
|
||||
|
||||
func test_asBase64UrlStringPadded() {
|
||||
let subject = Data(repeating: 1, count: 32)
|
||||
XCTAssertEqual(
|
||||
subject.base64UrlEncodedString(trimPadding: false),
|
||||
"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=",
|
||||
)
|
||||
}
|
||||
|
||||
func test_asBase64UrlStringUnpadded() {
|
||||
let subject = Data(repeating: 1, count: 32)
|
||||
XCTAssertEqual(
|
||||
subject.base64UrlEncodedString(trimPadding: false),
|
||||
"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE",
|
||||
)
|
||||
}
|
||||
|
||||
func test_fromBase64UrlStringPadded() {
|
||||
let subject = "9031WCEDOh6ZUGV_-wvUSw=="
|
||||
XCTAssertEqual(
|
||||
Data(base64UrlEncoded: subject)!,
|
||||
Data([0xf7, 0x4d, 0xf5, 0x58, 0x21, 0x03, 0x3a, 0x1e, 0x99, 0x50, 0x65, 0x7f, 0xfb, 0x0b, 0xd4, 0x4b]),
|
||||
)
|
||||
}
|
||||
|
||||
func test_fromBase64UrlStringUnpadded() {
|
||||
let subject = "9031WCEDOh6ZUGV_-wvUSw"
|
||||
XCTAssertEqual(
|
||||
Data(base64UrlEncoded: subject)!,
|
||||
Data([0xf7, 0x4d, 0xf5, 0x58, 0x21, 0x03, 0x3a, 0x1e, 0x99, 0x50, 0x65, 0x7f, 0xfb, 0x0b, 0xd4, 0x4b]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user