1 // Sha1Reg.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../C/Sha1.h"
6 
7 #include "../Common/MyBuffer2.h"
8 #include "../Common/MyCom.h"
9 
10 #include "../7zip/Common/RegisterCodec.h"
11 
12 class CSha1Hasher:
13   public IHasher,
14   public ICompressSetCoderProperties,
15   public CMyUnknownImp
16 {
17   CAlignedBuffer _buf;
18   Byte mtDummy[1 << 7];
19 
Sha()20   CSha1 *Sha() { return (CSha1 *)(void *)(Byte *)_buf; }
21 public:
CSha1Hasher()22   CSha1Hasher():
23     _buf(sizeof(CSha1))
24   {
25     Sha1_SetFunction(Sha(), 0);
26     Sha1_InitState(Sha());
27   }
28 
29   MY_UNKNOWN_IMP2(IHasher, ICompressSetCoderProperties)
30   INTERFACE_IHasher(;)
31   STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
32 };
33 
STDMETHODIMP_(void)34 STDMETHODIMP_(void) CSha1Hasher::Init() throw()
35 {
36   Sha1_InitState(Sha());
37 }
38 
STDMETHODIMP_(void)39 STDMETHODIMP_(void) CSha1Hasher::Update(const void *data, UInt32 size) throw()
40 {
41   Sha1_Update(Sha(), (const Byte *)data, size);
42 }
43 
STDMETHODIMP_(void)44 STDMETHODIMP_(void) CSha1Hasher::Final(Byte *digest) throw()
45 {
46   Sha1_Final(Sha(), digest);
47 }
48 
49 
SetCoderProperties(const PROPID * propIDs,const PROPVARIANT * coderProps,UInt32 numProps)50 STDMETHODIMP CSha1Hasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps)
51 {
52   unsigned algo = 0;
53   for (UInt32 i = 0; i < numProps; i++)
54   {
55     const PROPVARIANT &prop = coderProps[i];
56     if (propIDs[i] == NCoderPropID::kDefaultProp)
57     {
58       if (prop.vt != VT_UI4)
59         return E_INVALIDARG;
60       if (prop.ulVal > 2)
61         return E_NOTIMPL;
62       algo = (unsigned)prop.ulVal;
63     }
64   }
65   if (!Sha1_SetFunction(Sha(), algo))
66     return E_NOTIMPL;
67   return S_OK;
68 }
69 
70 REGISTER_HASHER(CSha1Hasher, 0x201, "SHA1", SHA1_DIGEST_SIZE)
71