tengs | 7df5c5c | 2015-02-25 07:21:35 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
khorimoto | 999e934c | 2016-11-18 20:10:42 | [diff] [blame] | 5 | #include "components/cryptauth/cryptauth_enrollment_utils.h" |
tengs | 7df5c5c | 2015-02-25 07:21:35 | [diff] [blame] | 6 | |
tengs | fd6f252f | 2015-09-01 19:17:59 | [diff] [blame] | 7 | #include <math.h> |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 8 | #include <stddef.h> |
tengs | fd6f252f | 2015-09-01 19:17:59 | [diff] [blame] | 9 | |
peter | e0807cf | 2015-11-20 14:46:24 | [diff] [blame] | 10 | #include "base/base64url.h" |
tengs | fd6f252f | 2015-09-01 19:17:59 | [diff] [blame] | 11 | #include "base/md5.h" |
tengs | 7df5c5c | 2015-02-25 07:21:35 | [diff] [blame] | 12 | #include "base/sha1.h" |
tengs | 7df5c5c | 2015-02-25 07:21:35 | [diff] [blame] | 13 | |
khorimoto | 999e934c | 2016-11-18 20:10:42 | [diff] [blame] | 14 | namespace cryptauth { |
tengs | 7df5c5c | 2015-02-25 07:21:35 | [diff] [blame] | 15 | |
tengs | fd6f252f | 2015-09-01 19:17:59 | [diff] [blame] | 16 | int64_t HashStringToInt64(const std::string& string) { |
| 17 | base::MD5Context context; |
| 18 | base::MD5Init(&context); |
| 19 | base::MD5Update(&context, string); |
| 20 | |
| 21 | base::MD5Digest digest; |
| 22 | base::MD5Final(&digest, &context); |
| 23 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 24 | // Fold the digest into an int64_t value. |digest.a| is a 16-byte array, so we |
tengs | fd6f252f | 2015-09-01 19:17:59 | [diff] [blame] | 25 | // sum the two 8-byte halves of the digest to create the hash. |
| 26 | int64_t hash = 0; |
| 27 | for (size_t i = 0; i < sizeof(digest.a); ++i) { |
| 28 | uint8_t byte = digest.a[i]; |
| 29 | hash += static_cast<int64_t>(byte) << (i % sizeof(int64_t)); |
| 30 | } |
| 31 | |
| 32 | return hash; |
| 33 | } |
| 34 | |
tengs | 7df5c5c | 2015-02-25 07:21:35 | [diff] [blame] | 35 | std::string CalculateDeviceUserId(const std::string& device_id, |
| 36 | const std::string& user_id) { |
| 37 | std::string device_user_id; |
peter | e0807cf | 2015-11-20 14:46:24 | [diff] [blame] | 38 | base::Base64UrlEncode(base::SHA1HashString(device_id + "|" + user_id), |
| 39 | base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 40 | &device_user_id); |
tengs | 7df5c5c | 2015-02-25 07:21:35 | [diff] [blame] | 41 | return device_user_id; |
| 42 | } |
| 43 | |
khorimoto | 999e934c | 2016-11-18 20:10:42 | [diff] [blame] | 44 | } // namespace cryptauth |