OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/ios/device_util.h" | 5 #include "base/ios/device_util.h" |
6 | 6 |
7 #include <CommonCrypto/CommonDigest.h> | 7 #include <CommonCrypto/CommonDigest.h> |
8 #import <UIKit/UIKit.h> | 8 #import <UIKit/UIKit.h> |
9 | 9 |
10 #include <ifaddrs.h> | 10 #include <ifaddrs.h> |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 // Try to migrate from legacy client id. | 40 // Try to migrate from legacy client id. |
41 NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey]; | 41 NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey]; |
42 | 42 |
43 // Some iOS6 devices return a buggy identifierForVendor: | 43 // Some iOS6 devices return a buggy identifierForVendor: |
44 // http://openradar.appspot.com/12377282. If this is the case, revert to | 44 // http://openradar.appspot.com/12377282. If this is the case, revert to |
45 // generating a new one. | 45 // generating a new one. |
46 if (!client_id || [client_id isEqualToString:kZeroUUID]) { | 46 if (!client_id || [client_id isEqualToString:kZeroUUID]) { |
47 if (base::ios::IsRunningOnIOS6OrLater()) { | 47 if (base::ios::IsRunningOnIOS6OrLater()) { |
48 client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; | 48 client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; |
49 if ([client_id isEqualToString:kZeroUUID]) | 49 if (!client_id || [client_id isEqualToString:kZeroUUID]) |
50 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); | 50 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); |
51 } else { | 51 } else { |
52 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); | 52 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); |
53 } | 53 } |
54 } | 54 } |
55 return client_id; | 55 return client_id; |
56 } | 56 } |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 150 |
151 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; | 151 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; |
152 | 152 |
153 if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) { | 153 if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) { |
154 client_id = GenerateClientId(); | 154 client_id = GenerateClientId(); |
155 [defaults setObject:client_id forKey:kClientIdPreferenceKey]; | 155 [defaults setObject:client_id forKey:kClientIdPreferenceKey]; |
156 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; | 156 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; |
157 [defaults synchronize]; | 157 [defaults synchronize]; |
158 } | 158 } |
159 | 159 |
160 return GetSaltedString([client_id UTF8String], salt ? salt : kDefaultSalt); | 160 return GetSaltedString(base::SysNSStringToUTF8(client_id), |
| 161 salt ? salt : kDefaultSalt); |
161 } | 162 } |
162 | 163 |
163 std::string GetSaltedString(const std::string& in_string, | 164 std::string GetSaltedString(const std::string& in_string, |
164 const std::string& salt) { | 165 const std::string& salt) { |
165 DCHECK(in_string.length()); | 166 DCHECK(in_string.length()); |
166 DCHECK(salt.length()); | 167 DCHECK(salt.length()); |
167 NSData* hash_data = | 168 NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt) |
168 [[NSString stringWithFormat:@"%s%s", in_string.c_str(), salt.c_str()] | 169 dataUsingEncoding:NSUTF8StringEncoding]; |
169 dataUsingEncoding:NSUTF8StringEncoding]; | |
170 | 170 |
171 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; | 171 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; |
172 CC_SHA256([hash_data bytes], [hash_data length], hash); | 172 CC_SHA256([hash_data bytes], [hash_data length], hash); |
173 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); | 173 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); |
174 | 174 |
175 base::ScopedCFTypeRef<CFUUIDRef> uuid_object( | 175 base::ScopedCFTypeRef<CFUUIDRef> uuid_object( |
176 CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); | 176 CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); |
177 base::ScopedCFTypeRef<CFStringRef> device_id( | 177 base::ScopedCFTypeRef<CFStringRef> device_id( |
178 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); | 178 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
179 return base::SysCFStringRefToUTF8(device_id); | 179 return base::SysCFStringRefToUTF8(device_id); |
180 } | 180 } |
181 | 181 |
182 } // namespace device_util | 182 } // namespace device_util |
183 } // namespace ios | 183 } // namespace ios |
OLD | NEW |