diff options
author | pkl@chromium.org <pkl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-03 14:27:18 +0000 |
---|---|---|
committer | pkl@chromium.org <pkl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-03 14:27:18 +0000 |
commit | 940b67dab7cc885657b16f81a67fd1ad784a3cf4 (patch) | |
tree | d17663e95019ea926789bb96615c64ef1a5bb09f /base/ios | |
parent | a1483f214dd8f7ad3751e3a79412bca9179429af (diff) | |
download | chromium_src-940b67dab7cc885657b16f81a67fd1ad784a3cf4.zip chromium_src-940b67dab7cc885657b16f81a67fd1ad784a3cf4.tar.gz chromium_src-940b67dab7cc885657b16f81a67fd1ad784a3cf4.tar.bz2 |
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
Diffstat (limited to 'base/ios')
-rw-r--r-- | base/ios/device_util.h | 6 | ||||
-rw-r--r-- | base/ios/device_util.mm | 12 | ||||
-rw-r--r-- | base/ios/device_util_unittest.mm | 27 |
3 files changed, 43 insertions, 2 deletions
diff --git a/base/ios/device_util.h b/base/ios/device_util.h index 93100da..4e69efc 100644 --- a/base/ios/device_util.h +++ b/base/ios/device_util.h @@ -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 diff --git a/base/ios/device_util.mm b/base/ios/device_util.mm index 1edcfb6c..8449537 100644 --- a/base/ios/device_util.mm +++ b/base/ios/device_util.mm @@ -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); diff --git a/base/ios/device_util_unittest.mm b/base/ios/device_util_unittest.mm index a0c9ec4..688acc1 100644 --- a/base/ios/device_util_unittest.mm +++ b/base/ios/device_util_unittest.mm @@ -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(); |