• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

CryptoHash.h

Go to the documentation of this file.
00001 # if ! defined CRYPTO_HASH_HEADER
00002 # define CRYPTO_HASH_HEADER
00003 
00004 /** @file
00005     Protocol class for crypto hashes.
00006 
00007     @section license License
00008 
00009     Licensed to the Apache Software Foundation (ASF) under one
00010     or more contributor license agreements.  See the NOTICE file
00011     distributed with this work for additional information
00012     regarding copyright ownership.  The ASF licenses this file
00013     to you under the Apache License, Version 2.0 (the
00014     "License"); you may not use this file except in compliance
00015     with the License.  You may obtain a copy of the License at
00016 
00017     http://www.apache.org/licenses/LICENSE-2.0
00018 
00019     Unless required by applicable law or agreed to in writing, software
00020     distributed under the License is distributed on an "AS IS" BASIS,
00021     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022     See the License for the specific language governing permissions and
00023     limitations under the License.
00024  */
00025 
00026 /// Apache Traffic Server commons.
00027 namespace ats {
00028   /// Crypto hash output.
00029   union CryptoHash {
00030     uint64_t b[2];   // Legacy placeholder
00031     uint64_t u64[2];
00032     uint32_t u32[4];
00033     uint8_t  u8[16];
00034 
00035     /// Default constructor - init to zero.
00036     CryptoHash() {
00037       u64[0] = 0;
00038       u64[1] = 0;
00039     }
00040 
00041     /// Assignment - bitwise copy.
00042     CryptoHash& operator = (CryptoHash const& that) {
00043       u64[0] = that.u64[0];
00044       u64[1] = that.u64[1];
00045       return *this;
00046     }
00047 
00048     /// Equality - bitwise identical.
00049     bool operator == (CryptoHash const& that) const {
00050       return u64[0] == that.u64[0] && u64[1] == that.u64[1];
00051     }
00052 
00053     /// Equality - bitwise identical.
00054     bool operator != (CryptoHash const& that) const {
00055       return !(*this == that);
00056     }
00057 
00058     /// Reduce to 64 bit value.
00059     uint64_t fold() const {
00060       return u64[0] ^ u64[1];
00061     }
00062 
00063     /// Access 64 bit slice.
00064     uint64_t operator [] (int i) const {
00065       return u64[i];
00066     }
00067 
00068     /// Access 64 bit slice.
00069     /// @note Identical to @ operator[] but included for symmetry.
00070     uint64_t slice64(int i) const {
00071       return u64[i];
00072     }
00073 
00074     /// Access 32 bit slice.
00075     uint32_t slice32(int i) const {
00076       return u32[i];
00077     }
00078 
00079     /// Fast conversion to hex in fixed sized string.
00080     char *toHexStr(char buffer[33]) {
00081       return ink_code_to_hex_str(buffer, u8);
00082     }
00083   };
00084 
00085   extern CryptoHash const CRYPTO_HASH_ZERO;
00086 
00087   /** Protocol class for a crypto hash context.
00088 
00089       A hash of this type is used for strong hashing, such as for URLs.
00090   */
00091   class CryptoContext {
00092     typedef CryptoContext self; ///< Self reference type.
00093   public:
00094 
00095     /// Destructor (force virtual)
00096     virtual ~CryptoContext() { }
00097 
00098     /// Update the hash with @a data of @a length bytes.
00099     virtual bool update(void const* data, int length) = 0;
00100     /// Finalize and extract the @a hash.
00101     virtual bool finalize(CryptoHash& hash) = 0;
00102 
00103     /// Convenience overload.
00104     bool finalize(CryptoHash* hash);
00105 
00106     /// Convenience - compute final @a hash for @a data.
00107     /// @note This is just as fast as the previous style, as a new context must be initialized
00108     /// everytime this is done.
00109     virtual bool hash_immediate(CryptoHash& hash, void const* data, int length);
00110   };
00111 
00112   inline bool CryptoContext::hash_immediate(CryptoHash& hash, void const* data, int length) {
00113     return this->update(data, length) && this->finalize(hash);
00114   }
00115   inline bool CryptoContext::finalize(CryptoHash* hash) { return this->finalize(*hash); }
00116 
00117 } // end namespace
00118 
00119 // Promote for the primitives who don't use namespaces...
00120 using ats::CryptoHash;
00121 using ats::CryptoContext;
00122 using ats::CRYPTO_HASH_ZERO;
00123 
00124 # endif // CRYPTO_HASH_HEADER

Generated by  doxygen 1.7.1