Commit 940b67da authored by pkl@chromium.org's avatar pkl@chromium.org

Refactors GetDeviceIdentifier()

This refactors GetDeviceIdentifier function into GetDeviceIdentifier
and GetSaltedString. GetSaltedString is a utility function that can
be used elsewhere.

Review URL: https://codereview.chromium.org/309963003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274520 0039d316-1c4b-4281-b951-d872f2087c98
parent a1483f21
......@@ -72,6 +72,12 @@ std::string GetRandomId();
// something that should be anonymous, you should probably pass NULL.
std::string GetDeviceIdentifier(const char* salt);
// Returns a hashed version of |in_string| using |salt| (which must not
// zero-length). Different salt values should result in differently hashed
// strings.
std::string GetSaltedString(const std::string& in_string,
const std::string& salt);
} // namespace device_util
} // namespace ios
......
......@@ -157,8 +157,16 @@ std::string GetDeviceIdentifier(const char* salt) {
[defaults synchronize];
}
NSData* hash_data = [[NSString stringWithFormat:@"%@%s", client_id,
salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding];
return GetSaltedString([client_id UTF8String], salt ? salt : kDefaultSalt);
}
std::string GetSaltedString(const std::string& in_string,
const std::string& salt) {
DCHECK(in_string.length());
DCHECK(salt.length());
NSData* hash_data =
[[NSString stringWithFormat:@"%s%s", in_string.c_str(), salt.c_str()]
dataUsingEncoding:NSUTF8StringEncoding];
unsigned char hash[CC_SHA256_DIGEST_LENGTH];
CC_SHA256([hash_data bytes], [hash_data length], hash);
......
......@@ -99,6 +99,33 @@ TEST_F(DeviceUtilTest, CheckMigrationFromZero) {
CleanNSUserDefaultsForDeviceId();
}
TEST_F(DeviceUtilTest, GetSaltedStringEquals) {
std::string string1("The quick brown fox jumps over the lazy dog");
std::string string2("The quick brown fox jumps over the lazy dog");
std::string salt("salt");
// Same string and same salt should result in the same salted string.
EXPECT_EQ(ios::device_util::GetSaltedString(string1, salt),
ios::device_util::GetSaltedString(string2, salt));
}
TEST_F(DeviceUtilTest, GetSaltedStringNotEquals) {
std::string string1("The quick brown fox jumps over the lazy dog");
std::string string2("The lazy brown fox jumps over the quick dog");
std::string salt("salt");
// Different string and same salt should result in different salted strings.
EXPECT_NE(ios::device_util::GetSaltedString(string1, salt),
ios::device_util::GetSaltedString(string2, salt));
}
TEST_F(DeviceUtilTest, GetSaltedStringDifferentSalt) {
std::string string1("The quick brown fox jumps over the lazy dog");
std::string salt1("salt");
std::string salt2("pepper");
// Same string with different salt should result in different salted strings.
EXPECT_NE(ios::device_util::GetSaltedString(string1, salt1),
ios::device_util::GetSaltedString(string1, salt2));
}
TEST_F(DeviceUtilTest, CheckDeviceMigration) {
CleanNSUserDefaultsForDeviceId();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment