Skip to content

Commit 5fe6687

Browse files
sichanyooSichan Yoo
andauthored
feat!: Bump CRT version to 0.37.0 & add CRC64NVME checksum algorithm wrapper (#845)
* Bump CRT version to 0.37.0 and add wrapper for new checksum algorithm. * Add doc comment re: checksum algorithms may expand in future. --------- Co-authored-by: Sichan Yoo <[email protected]>
1 parent 2855ff6 commit 5fe6687

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ let package = Package(
5555
],
5656
dependencies: {
5757
var dependencies: [Package.Dependency] = [
58-
.package(url: "https://github.com/awslabs/aws-crt-swift.git", exact: "0.36.0"),
58+
.package(url: "https://github.com/awslabs/aws-crt-swift.git", exact: "0.37.0"),
5959
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
6060
]
6161
let isDocCEnabled = ProcessInfo.processInfo.environment["AWS_SWIFT_SDK_ENABLE_DOCC"] != nil
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import protocol SmithyChecksumsAPI.Checksum
9+
import enum SmithyChecksumsAPI.HashResult
10+
import struct Foundation.Data
11+
import AwsCommonRuntimeKit
12+
13+
class CRC64NVME {
14+
let checksumName = "crc64nvme"
15+
let digestLength: Int = 8 // bytes
16+
17+
private var previousHash: UInt64
18+
19+
public init(previousHash: UInt64 = 0) {
20+
self.previousHash = previousHash
21+
}
22+
}
23+
24+
extension CRC64NVME: Checksum {
25+
func copy() -> any Checksum {
26+
return CRC64NVME(previousHash: previousHash)
27+
}
28+
29+
func update(chunk: Data) {
30+
self.previousHash = chunk.computeCRC64Nvme(previousCrc64Nvme: previousHash)
31+
}
32+
33+
func reset() {
34+
self.previousHash = UInt64()
35+
}
36+
37+
func digest() -> HashResult {
38+
return .integer64(previousHash)
39+
}
40+
}

Sources/SmithyChecksums/ChecksumAlgorithm.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extension ChecksumAlgorithm {
2424
switch string.lowercased() {
2525
case "crc32": return .crc32
2626
case "crc32c": return .crc32c
27+
case "crc64nvme": return .crc64nvme
2728
case "sha1": return .sha1
2829
case "sha256": return .sha256
2930
case "md5": return .md5 // md5 is not a valid flexible checksum algorithm
@@ -44,7 +45,7 @@ extension ChecksumAlgorithm {
4445

4546
public var isFlexibleChecksum: Bool {
4647
switch self {
47-
case .crc32, .crc32c, .sha256, .sha1:
48+
case .crc32, .crc32c, .crc64nvme, .sha256, .sha1:
4849
return true
4950
default:
5051
return false
@@ -57,6 +58,8 @@ extension ChecksumAlgorithm {
5758
return CRC32()
5859
case .crc32c:
5960
return CRC32C()
61+
case .crc64nvme:
62+
return CRC64NVME()
6063
case .sha1:
6164
return SHA1()
6265
case .sha256:
@@ -69,12 +72,12 @@ extension ChecksumAlgorithm {
6972

7073
extension ChecksumAlgorithm: Comparable {
7174
/*
72-
* Priority-order for validating checksum = [ CRC32C, CRC32, SHA1, SHA256 ]
75+
* Priority-order for validating checksum = [ CRC32C, CRC32, CRC64NVME, SHA1, SHA256 ]
7376
* Order is determined by speed of the algorithm's implementation
7477
* MD5 is not supported by list ordering
7578
*/
7679
public static func < (lhs: ChecksumAlgorithm, rhs: ChecksumAlgorithm) -> Bool {
77-
let order: [ChecksumAlgorithm] = [.crc32c, .crc32, .sha1, .sha256]
80+
let order: [ChecksumAlgorithm] = [.crc32c, .crc32, .crc64nvme, .sha1, .sha256]
7881

7982
let lhsIndex = order.firstIndex(of: lhs) ?? Int.max
8083
let rhsIndex = order.firstIndex(of: rhs) ?? Int.max
@@ -102,6 +105,18 @@ extension UInt32 {
102105
}
103106
}
104107

108+
extension UInt64 {
109+
public func toBase64EncodedString() -> String {
110+
// Create a Data instance from the UInt64 value
111+
let value = self
112+
var bigEndianValue = value.bigEndian
113+
let data = Data(bytes: &bigEndianValue, count: MemoryLayout<UInt64>.size)
114+
115+
// Base64 encode the data
116+
return data.base64EncodedString()
117+
}
118+
}
119+
105120
extension HashResult {
106121

107122
// Convert a HashResult to a hexadecimal String
@@ -111,6 +126,8 @@ extension HashResult {
111126
return data.map { String(format: "%02x", $0) }.joined()
112127
case .integer(let integer):
113128
return String(format: "%08x", integer)
129+
case .integer64(let integer64):
130+
return String(format: "%016x", integer64)
114131
}
115132
}
116133

@@ -121,6 +138,8 @@ extension HashResult {
121138
return data.base64EncodedString()
122139
case .integer(let integer):
123140
return integer.toBase64EncodedString()
141+
case .integer64(let integer64):
142+
return integer64.toBase64EncodedString()
124143
}
125144
}
126145
}

Sources/SmithyChecksumsAPI/Checksum.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ public protocol Checksum {
4545
public enum HashResult {
4646
case data(Data)
4747
case integer(UInt32)
48+
case integer64(UInt64)
4849
}

Sources/SmithyChecksumsAPI/ChecksumAlgorithm.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
/// The list of checksum algorithms contained by this enum may expand in future.
89
public enum ChecksumAlgorithm {
9-
case crc32, crc32c, sha1, sha256, md5
10+
case crc32, crc32c, crc64nvme, sha1, sha256, md5
1011

1112
public func toString() -> String {
1213
switch self {
1314
case .crc32: return "crc32"
1415
case .crc32c: return "crc32c"
16+
case .crc64nvme: return "crc64nvme"
1517
case .sha1: return "sha1"
1618
case .sha256: return "sha256"
1719
case .md5: return "md5"

0 commit comments

Comments
 (0)