-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
UInt32(littleEndian32Bits:)
and UInt32.littleEndianData
This will be used for extracting intermediate key expiration from the new signature format.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 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,30 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// Integer+Extensions.swift | ||
// | ||
// Created by Nacho Soto on 6/26/23. | ||
|
||
import Foundation | ||
|
||
extension UInt32 { | ||
|
||
/// Converts 32 bits of little-endian `Data` into a `UInt32`. | ||
init(littleEndian32Bits data: Data) { | ||
assert(data.count == 4, "Data needs to be 32bits: \(data)") | ||
|
||
self.init(littleEndian: data.withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
} | ||
|
||
/// - Returns: the `Data` representation as little-endian 32 bits. | ||
var littleEndianData: Data { | ||
return Data(withUnsafeBytes(of: self.littleEndian, Array.init)) | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
Tests/UnitTests/FoundationExtensions/IntegerExtensionsTests.swift
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,60 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// IntegerExtensionsTests.swift | ||
// | ||
// Created by Nacho Soto on 6/26/23. | ||
|
||
import Nimble | ||
import XCTest | ||
|
||
@testable import RevenueCat | ||
|
||
class IntegerExtensionsTests: TestCase { | ||
|
||
func testParseUInt32Data() { | ||
expect(UInt32(littleEndian32Bits: Data([0x00, 0x00, 0x00, 0x00]))) == 0 | ||
expect(UInt32(littleEndian32Bits: Data([0x01, 0x00, 0x00, 0x00]))) == 1 | ||
expect(UInt32(littleEndian32Bits: Data([0xff, 0x00, 0x00, 0x00]))) == 255 | ||
expect(UInt32(littleEndian32Bits: Data([0xff, 0xff, 0xff, 0xff]))) == UInt32(2 ^^ 32 - 1) | ||
} | ||
|
||
func testUInt32ToData() { | ||
expect(UInt32(0).littleEndianData) == Data([0x00, 0x00, 0x00, 0x00]) | ||
expect(UInt32(1).littleEndianData) == Data([0x01, 0x00, 0x00, 0x00]) | ||
expect(UInt32(255).littleEndianData) == Data([0xff, 0x00, 0x00, 0x00]) | ||
expect(UInt32(2 ^^ 32 - 1).littleEndianData) == Data([0xff, 0xff, 0xff, 0xff]) | ||
} | ||
|
||
func testUInt32BidirectionalConversion() { | ||
expect(UInt32(0).encodeAndDecode()) == 0 | ||
expect(UInt32(1).encodeAndDecode()) == 1 | ||
expect(UInt32(255).encodeAndDecode()) == 255 | ||
expect(UInt32(2 ^^ 32 - 1).encodeAndDecode()) == UInt32(2 ^^ 32 - 1) | ||
} | ||
|
||
} | ||
|
||
private extension UInt32 { | ||
|
||
func encodeAndDecode() -> Self { | ||
return UInt32(littleEndian32Bits: self.littleEndianData) | ||
} | ||
|
||
} | ||
|
||
// MARK: - | ||
|
||
precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence } | ||
infix operator ^^ : PowerPrecedence | ||
|
||
/// Returns `radis` raised to `power`. | ||
private func ^^ (radix: Int, power: Int) -> Int { | ||
return Int(pow(Double(radix), Double(power))) | ||
} |