Md5.hxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:5k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. #ifndef MD5_HXX_
  2. #define MD5_HXX_
  3. /* ====================================================================
  4.  * The Vovida Software License, Version 1.0 
  5.  * 
  6.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  7.  * 
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in
  17.  *    the documentation and/or other materials provided with the
  18.  *    distribution.
  19.  * 
  20.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  21.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  22.  *    not be used to endorse or promote products derived from this
  23.  *    software without prior written permission. For written
  24.  *    permission, please contact vocal@vovida.org.
  25.  *
  26.  * 4. Products derived from this software may not be called "VOCAL", nor
  27.  *    may "VOCAL" appear in their name, without prior written
  28.  *    permission of Vovida Networks, Inc.
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  31.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  33.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  34.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  35.  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  36.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  37.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  38.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  41.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42.  * DAMAGE.
  43.  * 
  44.  * ====================================================================
  45.  * 
  46.  * This software consists of voluntary contributions made by Vovida
  47.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  48.  * Inc.  For more information on Vovida Networks, Inc., please see
  49.  * <http://www.vovida.org/>.
  50.  *
  51.  */
  52. static const char* const Md5_hxx_version =
  53.     "$Id: Md5.hxx,v 1.1 2001/03/26 10:35:31 icahoon Exp $";
  54. #include "Data.hxx"
  55. #include "vmd5.h"
  56. /** Infrastructure common to VOCAL.
  57.  */
  58. namespace Vocal 
  59. {
  60. /** Infrastructure common to VOCAL to do cryptographic things.
  61.  */
  62. namespace Crypto
  63. {
  64. /** class to implement the MD5 message digest algorithm.
  65.  */
  66. class Md5
  67. {
  68.     public:
  69. /// constructor
  70. Md5();
  71. /// copy constructor -- returns the same state as the original
  72. Md5(const Md5& src);
  73. /// initialize and take a data
  74. Md5(const Data& data);
  75. /// equality operator 
  76. bool operator==(const Md5& src) const;
  77. /// less than operator
  78. bool operator<(const Md5& src) const;
  79. /// greater than operator
  80. bool operator>(const Md5& src) const;
  81. /// set the digest in an empty md5
  82. void digest(const Data& data);
  83. /// hash function
  84. size_t hashfn() const;
  85. /// destructor
  86. ~Md5();
  87.     private:
  88. MD5Context myContext;
  89. unsigned char myDigest[16];
  90. };
  91. inline
  92. Md5::Md5()
  93. {
  94.     memset(myDigest, 0, 16);
  95. }
  96. inline
  97. Md5::Md5(const Md5& src)
  98. {
  99.     memcpy(myDigest, src.myDigest, 16);
  100. }
  101. inline
  102. Md5::Md5(const Data& data)
  103. {
  104.     MD5Init(&myContext);
  105.     MD5Update(&myContext, 
  106.       reinterpret_cast<const unsigned char*>(data.getDataBuf()), 
  107.       static_cast<unsigned int>(data.length()));
  108.     MD5Final(myDigest, &myContext);
  109. }
  110. inline void
  111. Md5::digest(const Data& data)
  112. {
  113.     MD5Init(&myContext);
  114.     MD5Update(&myContext, 
  115.       reinterpret_cast<const unsigned char*>(data.getDataBuf()), 
  116.       static_cast<unsigned int>(data.length()));
  117.     MD5Final(myDigest, &myContext);
  118. }
  119. inline size_t
  120. Md5::hashfn() const
  121. {
  122.     return *reinterpret_cast<size_t const*>(myDigest);
  123. }
  124. inline
  125. Md5::~Md5()
  126. {
  127. }
  128. inline bool
  129. Md5::operator==(const Md5& src) const
  130. {
  131.     if(&src == this)
  132.     {
  133. return true;
  134.     }
  135.     else
  136.     {
  137. return (memcmp(&myDigest, &src.myDigest, 16) == 0);
  138.     }
  139. }
  140. inline bool
  141. Md5::operator<(const Md5& src) const
  142. {
  143.     if(&src == this)
  144.     {
  145. return false;
  146.     }
  147.     else
  148.     {
  149. return (memcmp(&myDigest, &src.myDigest, 16) < 0);
  150.     }
  151. }
  152. inline bool
  153. Md5::operator>(const Md5& src) const
  154. {
  155.     if(&src == this)
  156.     {
  157. return false;
  158.     }
  159.     else
  160.     {
  161. return (memcmp(&myDigest, &src.myDigest, 16) > 0);
  162.     }
  163. }
  164. } // namespace Crypto
  165. } // namespace Vocal
  166. #endif