blob: e8429532ffe121a29617fab9445eafee757651a7 [file] [log] [blame]
tengs7df5c5c2015-02-25 07:21:351// 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
khorimoto999e934c2016-11-18 20:10:425#include "components/cryptauth/cryptauth_enrollment_utils.h"
tengs7df5c5c2015-02-25 07:21:356
tengsfd6f252f2015-09-01 19:17:597#include <math.h>
avif57136c12015-12-25 23:27:458#include <stddef.h>
tengsfd6f252f2015-09-01 19:17:599
petere0807cf2015-11-20 14:46:2410#include "base/base64url.h"
tengsfd6f252f2015-09-01 19:17:5911#include "base/md5.h"
tengs7df5c5c2015-02-25 07:21:3512#include "base/sha1.h"
tengs7df5c5c2015-02-25 07:21:3513
khorimoto999e934c2016-11-18 20:10:4214namespace cryptauth {
tengs7df5c5c2015-02-25 07:21:3515
tengsfd6f252f2015-09-01 19:17:5916int64_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
avif57136c12015-12-25 23:27:4524 // Fold the digest into an int64_t value. |digest.a| is a 16-byte array, so we
tengsfd6f252f2015-09-01 19:17:5925 // 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
tengs7df5c5c2015-02-25 07:21:3535std::string CalculateDeviceUserId(const std::string& device_id,
36 const std::string& user_id) {
37 std::string device_user_id;
petere0807cf2015-11-20 14:46:2438 base::Base64UrlEncode(base::SHA1HashString(device_id + "|" + user_id),
39 base::Base64UrlEncodePolicy::INCLUDE_PADDING,
40 &device_user_id);
tengs7df5c5c2015-02-25 07:21:3541 return device_user_id;
42}
43
khorimoto999e934c2016-11-18 20:10:4244} // namespace cryptauth