-
Notifications
You must be signed in to change notification settings - Fork 18
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
Showing
6 changed files
with
139 additions
and
34 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,29 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
|
||
import struct Foundation.Data | ||
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?.assumingMemoryBound(to: UInt8.self), | ||
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?.assumingMemoryBound(to: UInt8.self), | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0. | ||
import struct Foundation.Date | ||
import struct Foundation.Data | ||
import struct Foundation.TimeInterval | ||
import AwsCCal | ||
|
||
extension String { | ||
|
||
/// Computes the md5 hash over input and writes the digest output to 'output'. Use this if you don't need to stream the data you're hashing and you can load | ||
/// the entire input to hash into memory. | ||
/// - Parameter truncate: If you specify truncate something other than 0, the output will be truncated to that number of bytes. | ||
public func base64EncodedMD5(truncate: Int = 0) throws -> String { | ||
let bufferSize = 16 | ||
var bufferData = Data(count: bufferSize) | ||
try bufferData.withUnsafeMutableBytes { bufferPointer in | ||
var buffer = aws_byte_buf_from_empty_array(bufferPointer.baseAddress, bufferSize) | ||
guard self.withByteCursorPointer({ strCursorPointer in | ||
aws_md5_compute(allocator.rawValue, strCursorPointer, &buffer, truncate) | ||
}) == AWS_OP_SUCCESS else { | ||
throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
} | ||
} | ||
return bufferData.base64EncodedString() | ||
} | ||
} | ||
|
||
extension Data { | ||
|
||
/// Computes the sha256 hash over data. | ||
/// - Parameter truncate: If you specify truncate something other than 0, the output will be truncated to that number of bytes. For | ||
/// example, if you want a SHA256 digest as the first 16 bytes, set truncate to 16. | ||
public func sha256(truncate: Int = 0) throws -> Data { | ||
try self.withUnsafeBytes { bufferPointer in | ||
var byteCursor = aws_byte_cursor_from_array(bufferPointer.baseAddress, count) | ||
let bufferSize = Int(AWS_SHA256_LEN) | ||
var bufferData = Data(count: bufferSize) | ||
try bufferData.withUnsafeMutableBytes { bufferDataPointer in | ||
var buffer = aws_byte_buf_from_empty_array(bufferDataPointer.baseAddress, bufferSize) | ||
guard aws_sha256_compute(allocator.rawValue, &byteCursor, &buffer, truncate) == AWS_OP_SUCCESS else { | ||
throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
} | ||
} | ||
return bufferData | ||
} | ||
} | ||
|
||
/// Computes the sha1 hash over data. | ||
/// - Parameter truncate: If you specify truncate something other than 0, the output will be truncated to that number of bytes. For | ||
/// example, if you want a SHA1 digest as the first 10 bytes, set truncate to 10. | ||
public func sha1(truncate: Int = 0) throws -> Data { | ||
try self.withUnsafeBytes { bufferPointer in | ||
var byteCursor = aws_byte_cursor_from_array(bufferPointer.baseAddress, count) | ||
let bufferSize = Int(AWS_SHA1_LEN) | ||
var bufferData = Data(count: bufferSize) | ||
try bufferData.withUnsafeMutableBytes { bufferDataPointer in | ||
var buffer = aws_byte_buf_from_empty_array(bufferDataPointer.baseAddress, bufferSize) | ||
guard aws_sha1_compute(allocator.rawValue, &byteCursor, &buffer, truncate) == AWS_OP_SUCCESS else { | ||
throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
} | ||
} | ||
return bufferData | ||
} | ||
} | ||
} |
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,21 @@ | ||
// 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