Skip to content

Commit

Permalink
Add MD5
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Feb 6, 2024
1 parent 73c5392 commit 586667c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Source/AwsCommonRuntimeKit/crt/Hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import AwsCCal
public enum HashAlgorithm {
case SHA1
case SHA256
case MD5

var size: Int {
switch self {
case .SHA1:
return Int(AWS_SHA1_LEN)
case .SHA256:
return Int(AWS_SHA256_LEN)
case .MD5:
return Int(AWS_MD5_LEN)
}
}
}
Expand All @@ -29,6 +32,8 @@ public class Hash {
rawValue = aws_sha1_new(allocator.rawValue)
case .SHA256:
rawValue = aws_sha256_new(allocator.rawValue)
case .MD5:
rawValue = aws_md5_new(allocator.rawValue)
}
}

Expand Down
18 changes: 18 additions & 0 deletions Test/AwsCommonRuntimeKitTests/crt/HashTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class HashTests: XCBaseTestCase {

XCTAssertEqual(md5, "iB0/3YSo7maijL0IGOgA9g==")
}

func testMd5PayloadWithUpdate() throws {
let hello = "Hello".data(using: .utf8)!
let world = "World".data(using: .utf8)!

let helloWorld = "HelloWorld".data(using: .utf8)!
let md5 = try! helloWorld.computeMD5().encodeToHexString()

let hash = Hash(algorithm: .MD5)
try hash.update(data: hello)
try hash.update(data: world)
let finalizedHash = try hash.finalize().encodeToHexString()


XCTAssertEqual(md5, finalizedHash)
XCTAssertEqual(finalizedHash, "68e109f0f40ca72a15e05cc22786f8e6")
}


func testSha256() throws {
let hello = "Hello".data(using: .utf8)!
Expand Down

0 comments on commit 586667c

Please sign in to comment.