-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sha1, crc32 and crc32c implementation
- Loading branch information
Showing
5 changed files
with
97 additions
and
3 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,31 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import Foundation | ||
|
||
import struct Foundation.Date | ||
import struct Foundation.Data | ||
import struct Foundation.TimeInterval | ||
import AwsCCal | ||
import AwsCChecksums | ||
|
||
extension Data { | ||
|
||
/// Computes the CRC32 over data. | ||
/// - Parameter previousCrc32: Pass 0 in the previousCrc32 parameter as an initial value unless continuing to update a running crc in a subsequent call. | ||
public func crc32(previousCrc32: UInt32 = 0) -> UInt32 { | ||
self.withUnsafeBytes { bufferPointer in | ||
return aws_checksums_crc32(bufferPointer.baseAddress, Int32(count) , previousCrc32) | ||
} | ||
} | ||
|
||
/// Computes the crc32c over data. | ||
/// - Parameter previousCrc32c: Pass 0 in the previousCrc32c parameter as an initial value unless continuing to update a running crc in a subsequent call. | ||
public func crc32c(previousCrc32c: UInt32 = 0) -> UInt32 { | ||
self.withUnsafeBytes { bufferPointer in | ||
return aws_checksums_crc32c(bufferPointer.baseAddress, Int32(count) , previousCrc32c) | ||
} | ||
} | ||
|
||
|
||
} |
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,23 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
import XCTest | ||
import AwsCCommon | ||
@testable import AwsCommonRuntimeKit | ||
|
||
|
||
class ChecksumsTests: XCBaseTestCase { | ||
|
||
func testCRC32() throws { | ||
XCTAssertEqual("".data(using: .utf8)!.crc32(), 0) | ||
XCTAssertEqual("Hello".data(using: .utf8)!.crc32(), 4157704578) | ||
XCTAssertEqual("{\"foo\":\"base64 encoded sha1 checksum\"}".data(using: .utf8)!.crc32(), 1195144130) | ||
} | ||
|
||
|
||
func testCRC32C() throws { | ||
XCTAssertEqual("".data(using: .utf8)!.crc32c(), 0) | ||
XCTAssertEqual("Hello".data(using: .utf8)!.crc32c(), 2178485787) | ||
XCTAssertEqual("{\"foo\":\"base64 encoded sha1 checksum\"}".data(using: .utf8)!.crc32c(), 3565301023) | ||
} | ||
|
||
} |
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