15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef CHROME_BROWSER_PREFS_PREF_HASH_CALCULATOR_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CHROME_BROWSER_PREFS_PREF_HASH_CALCULATOR_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace base {
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class Value;
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace base
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Calculates and validates preference value hashes.
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class PrefHashCalculator {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum ValidationResult {
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    INVALID,
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VALID,
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Valid under a deprecated but as secure algorithm.
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    VALID_SECURE_LEGACY,
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Constructs a PrefHashCalculator using |seed| and |device_id|. The same
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // parameters must be used in order to successfully validate generated hashes.
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |device_id| may be empty.
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PrefHashCalculator(const std::string& seed, const std::string& device_id);
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ~PrefHashCalculator();
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Calculates a hash value for the supplied preference |path| and |value|.
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |value| may be null if the preference has no value.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string Calculate(const std::string& path, const base::Value* value)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Validates the provided preference hash using current and legacy hashing
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // algorithms.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ValidationResult Validate(const std::string& path,
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            const base::Value* value,
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            const std::string& hash) const;
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::string seed_;
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::string device_id_;
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const std::string legacy_device_id_;
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PrefHashCalculator);
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // CHROME_BROWSER_PREFS_PREF_HASH_CALCULATOR_H_
53