Fixed MD5 and SHA256 bug in apple platform implementation

PiperOrigin-RevId: 776303688
This commit is contained in:
Guogang Li
2025-06-26 15:26:17 -07:00
committed by Copybara-Service
parent 2426f3e174
commit 2df88546be
2 changed files with 45 additions and 25 deletions
@@ -16,39 +16,56 @@
#include <string>
#include "internal/platform/implementation/crypto.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/crypto.h"
@interface GNCCryptoTest : XCTestCase
@end
@implementation GNCCryptoTest
// Tests that the hash functions return the expected values.
- (void)testHashValues {
- (void)testSHA256HashOnString {
std::string string("com.google.location.nearby");
const uint8_t sha256ExpectedHash[] = {
0x09, 0x8e, 0x3c, 0x54, 0x2d, 0xee, 0x9e, 0x35, 0x1d, 0xe7, 0x5b,
0xcf, 0xda, 0xb5, 0x62, 0xd3, 0xde, 0xba, 0x14, 0x49, 0xbd, 0xf5,
0x95, 0x04, 0x1f, 0x1d, 0x99, 0x84, 0x87, 0xc3, 0xcb, 0x8a,
};
nearby::ByteArray sha256Hash = nearby::Crypto::Sha256(string);
XCTAssert(sha256Hash.size() == sizeof(sha256ExpectedHash));
XCTAssert(memcmp(sha256Hash.data(), sha256ExpectedHash, sizeof(sha256ExpectedHash)) == 0);
}
// SHA-256 test.
{
const uint8_t sha256ExpectedHash[] = {
0x09, 0x8e, 0x3c, 0x54, 0x2d, 0xee, 0x9e, 0x35,
0x1d, 0xe7, 0x5b, 0xcf, 0xda, 0xb5, 0x62, 0xd3,
0xde, 0xba, 0x14, 0x49, 0xbd, 0xf5, 0x95, 0x04,
0x1f, 0x1d, 0x99, 0x84, 0x87, 0xc3, 0xcb, 0x8a, };
nearby::ByteArray sha256Hash = nearby::Crypto::Sha256(string);
XCTAssert(sha256Hash.size() == sizeof(sha256ExpectedHash) &&
memcmp(sha256Hash.data(), sha256ExpectedHash, sizeof(sha256ExpectedHash)) == 0);
}
- (void)testMD5HashOnString {
std::string string("com.google.location.nearby");
const uint8_t md5ExpectedHash[] = {0x97, 0x78, 0x1d, 0x3d, 0xee, 0xd7, 0xdc, 0x5a,
0x6e, 0xee, 0x50, 0x08, 0xce, 0xd1, 0xb2, 0xe8};
nearby::ByteArray md5Hash = nearby::Crypto::Md5(string);
XCTAssert(md5Hash.size() == sizeof(md5ExpectedHash));
XCTAssert(memcmp(md5Hash.data(), md5ExpectedHash, sizeof(md5ExpectedHash)) == 0);
}
// MD5 test.
{
const uint8_t md5ExpectedHash[] = {
0x97, 0x78, 0x1d, 0x3d, 0xee, 0xd7, 0xdc, 0x5a,
0x6e, 0xee, 0x50, 0x08, 0xce, 0xd1, 0xb2, 0xe8 };
nearby::ByteArray md5Hash = nearby::Crypto::Md5(string);
XCTAssert(md5Hash.size() == sizeof(md5ExpectedHash) &&
memcmp(md5Hash.data(), md5ExpectedHash, sizeof(md5ExpectedHash)) == 0);
}
- (void)testSHA256HashOnBytes {
std::string bytes("\x4a\x17\x23\x33\x54\x53\x35\x11\x32\x8a\x59\xac\x9b\x6f\x57\x53\x47\x46"
"\xb6\x1f\x40\x1a\x7e\x74\xcb\x35\x0e");
const uint8_t sha256ExpectedHash[] = {
0xc7, 0x0e, 0x0f, 0x78, 0xb2, 0xbc, 0x9f, 0x32, 0x52, 0xb5, 0xe3,
0x6c, 0x9d, 0x2c, 0x38, 0x2e, 0xe6, 0x09, 0xd7, 0xce, 0x86, 0x8a,
0x29, 0xbb, 0x8e, 0xed, 0x6f, 0xbe, 0xdc, 0x19, 0xef, 0x6e,
};
nearby::ByteArray sha256Hash = nearby::Crypto::Sha256(bytes);
XCTAssert(sha256Hash.size() == sizeof(sha256ExpectedHash));
XCTAssert(memcmp(sha256Hash.data(), sha256ExpectedHash, sizeof(sha256ExpectedHash)) == 0);
}
- (void)testMD5HashOnBytes {
std::string bytes("\x4a\x17\x23\x33\x54\x53\x35\x11\x32\x8a\x59\xac\x9b\x6f\x57\x53\x47\x46"
"\xb6\x1f\x40\x1a\x7e\x74\xcb\x35\x0e");
const uint8_t md5ExpectedHash[] = {0x72, 0x06, 0xdb, 0x47, 0x53, 0xb9, 0xe1, 0x2a,
0xcb, 0x00, 0xea, 0x18, 0xc3, 0x11, 0x59, 0x1f};
nearby::ByteArray md5Hash = nearby::Crypto::Md5(bytes);
XCTAssert(md5Hash.size() == sizeof(md5ExpectedHash));
XCTAssert(memcmp(md5Hash.data(), md5ExpectedHash, sizeof(md5ExpectedHash)) == 0);
}
@end
@@ -14,6 +14,8 @@
#include "internal/platform/implementation/crypto.h"
#import <Foundation/Foundation.h>
#import "absl/strings/string_view.h"
#import "internal/platform/implementation/apple/GNCUtils.h"
#import "internal/platform/implementation/apple/utils.h"
@@ -25,13 +27,14 @@ void Crypto::Init() {}
ByteArray Crypto::Md5(absl::string_view input) {
if (input.empty()) return ByteArray();
return ByteArrayFromNSData(GNCMd5String(ObjCStringFromCppString(input)));
return ByteArrayFromNSData(GNCMd5Data([NSData dataWithBytes:input.data() length:input.size()]));
}
ByteArray Crypto::Sha256(absl::string_view input) {
if (input.empty()) return ByteArray();
return ByteArrayFromNSData(GNCSha256String(ObjCStringFromCppString(input)));
return ByteArrayFromNSData(GNCSha256Data([NSData dataWithBytes:input.data()
length:input.size()]));
}
} // namespace nearby