1 // Sha256Reg.cpp 2 3 #include "StdAfx.h" 4 5 #include "../../C/Sha256.h" 6 7 #include "../Common/MyBuffer2.h" 8 #include "../Common/MyCom.h" 9 10 #include "../7zip/Common/RegisterCodec.h" 11 12 Z7_CLASS_IMP_COM_2( 13 CSha256Hasher 14 , IHasher 15 , ICompressSetCoderProperties 16 ) 17 CAlignedBuffer1 _buf; 18 public: 19 Byte _mtDummy[1 << 7]; 20 21 CSha256 *Sha() { return (CSha256 *)(void *)(Byte *)_buf; } 22 public: 23 CSha256Hasher(): 24 _buf(sizeof(CSha256)) 25 { 26 Sha256_SetFunction(Sha(), 0); 27 Sha256_InitState(Sha()); 28 } 29 }; 30 31 Z7_COM7F_IMF2(void, CSha256Hasher::Init()) 32 { 33 Sha256_InitState(Sha()); 34 } 35 36 Z7_COM7F_IMF2(void, CSha256Hasher::Update(const void *data, UInt32 size)) 37 { 38 Sha256_Update(Sha(), (const Byte *)data, size); 39 } 40 41 Z7_COM7F_IMF2(void, CSha256Hasher::Final(Byte *digest)) 42 { 43 Sha256_Final(Sha(), digest); 44 } 45 46 47 Z7_COM7F_IMF(CSha256Hasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps)) 48 { 49 unsigned algo = 0; 50 for (UInt32 i = 0; i < numProps; i++) 51 { 52 if (propIDs[i] == NCoderPropID::kDefaultProp) 53 { 54 const PROPVARIANT &prop = coderProps[i]; 55 if (prop.vt != VT_UI4) 56 return E_INVALIDARG; 57 if (prop.ulVal > 2) 58 return E_NOTIMPL; 59 algo = (unsigned)prop.ulVal; 60 } 61 } 62 if (!Sha256_SetFunction(Sha(), algo)) 63 return E_NOTIMPL; 64 return S_OK; 65 } 66 67 REGISTER_HASHER(CSha256Hasher, 0xA, "SHA256", SHA256_DIGEST_SIZE) 68