]>
git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/SHA1_MD5_Object.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * DigestObject.cpp - generic C++ implementations of SHA1 and MD5.
22 * Created 2/19/2001 by dmitch.
25 #include "SHA1_MD5_Object.h"
32 void MD5Object::digestInit()
38 void MD5Object::digestUpdate(
43 throw std::runtime_error("MD5 digestUpdate after final");
45 MD5Update(&mCtx
, (unsigned char *)data
, len
);
48 void MD5Object::digestFinal(
52 throw std::runtime_error("MD5 digestFinal after final");
54 MD5Final(&mCtx
, (unsigned char *)digest
);
58 /* use default memberwise init */
59 DigestObject
*MD5Object::digestClone() const
61 return new MD5Object(*this);
64 UInt32
MD5Object::digestSizeInBytes() const
66 return MD5_DIGEST_SIZE
;
72 void SHA1Object::digestInit()
79 void SHA1Object::digestUpdate(
84 uint8
*uData
= (uint8
*)data
;
87 throw std::runtime_error("SHA1 digestUpdate after final");
90 // deal with miniscule input leaving still less than one block
91 if (mBufferCount
+ len
< SHS_BLOCKSIZE
) {
92 memcpy(mBuffer
+ mBufferCount
, uData
, len
);
97 // fill possible partial existing buffer and process
98 if (mBufferCount
> 0) {
99 cnt
= SHS_BLOCKSIZE
- mBufferCount
;
100 memcpy(mBuffer
+ mBufferCount
, uData
, cnt
);
101 shsUpdate(&mCtx
, mBuffer
, SHS_BLOCKSIZE
);
106 // process remaining whole buffer multiples
107 UInt32 blocks
= len
/ SHS_BLOCKSIZE
;
109 cnt
= blocks
* SHS_BLOCKSIZE
;
110 shsUpdate(&mCtx
, uData
, cnt
);
118 memcpy(mBuffer
, uData
, len
);
122 void SHA1Object::digestFinal(
126 throw std::runtime_error("SHA1 digestFinal after final");
128 if (mBufferCount
> 0) {
129 shsUpdate(&mCtx
, mBuffer
, mBufferCount
);
132 memcpy(digest
, mCtx
.digest
, SHS_DIGESTSIZE
);
136 /* use default memberwise init */
137 DigestObject
*SHA1Object::digestClone() const
139 return new SHA1Object(*this);
142 UInt32
SHA1Object::digestSizeInBytes() const
144 return SHS_DIGESTSIZE
;