1370b324cSopenharmony_ci// Sha256Reg.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "../../C/Sha256.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#include "../Common/MyBuffer2.h"
8370b324cSopenharmony_ci#include "../Common/MyCom.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci#include "../7zip/Common/RegisterCodec.h"
11370b324cSopenharmony_ci
12370b324cSopenharmony_ciZ7_CLASS_IMP_COM_2(
13370b324cSopenharmony_ci  CSha256Hasher
14370b324cSopenharmony_ci  , IHasher
15370b324cSopenharmony_ci  , ICompressSetCoderProperties
16370b324cSopenharmony_ci)
17370b324cSopenharmony_ci  CAlignedBuffer1 _buf;
18370b324cSopenharmony_cipublic:
19370b324cSopenharmony_ci  Byte _mtDummy[1 << 7];
20370b324cSopenharmony_ci
21370b324cSopenharmony_ci  CSha256 *Sha() { return (CSha256 *)(void *)(Byte *)_buf; }
22370b324cSopenharmony_cipublic:
23370b324cSopenharmony_ci  CSha256Hasher():
24370b324cSopenharmony_ci    _buf(sizeof(CSha256))
25370b324cSopenharmony_ci  {
26370b324cSopenharmony_ci    Sha256_SetFunction(Sha(), 0);
27370b324cSopenharmony_ci    Sha256_InitState(Sha());
28370b324cSopenharmony_ci  }
29370b324cSopenharmony_ci};
30370b324cSopenharmony_ci
31370b324cSopenharmony_ciZ7_COM7F_IMF2(void, CSha256Hasher::Init())
32370b324cSopenharmony_ci{
33370b324cSopenharmony_ci  Sha256_InitState(Sha());
34370b324cSopenharmony_ci}
35370b324cSopenharmony_ci
36370b324cSopenharmony_ciZ7_COM7F_IMF2(void, CSha256Hasher::Update(const void *data, UInt32 size))
37370b324cSopenharmony_ci{
38370b324cSopenharmony_ci  Sha256_Update(Sha(), (const Byte *)data, size);
39370b324cSopenharmony_ci}
40370b324cSopenharmony_ci
41370b324cSopenharmony_ciZ7_COM7F_IMF2(void, CSha256Hasher::Final(Byte *digest))
42370b324cSopenharmony_ci{
43370b324cSopenharmony_ci  Sha256_Final(Sha(), digest);
44370b324cSopenharmony_ci}
45370b324cSopenharmony_ci
46370b324cSopenharmony_ci
47370b324cSopenharmony_ciZ7_COM7F_IMF(CSha256Hasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps))
48370b324cSopenharmony_ci{
49370b324cSopenharmony_ci  unsigned algo = 0;
50370b324cSopenharmony_ci  for (UInt32 i = 0; i < numProps; i++)
51370b324cSopenharmony_ci  {
52370b324cSopenharmony_ci    if (propIDs[i] == NCoderPropID::kDefaultProp)
53370b324cSopenharmony_ci    {
54370b324cSopenharmony_ci      const PROPVARIANT &prop = coderProps[i];
55370b324cSopenharmony_ci      if (prop.vt != VT_UI4)
56370b324cSopenharmony_ci        return E_INVALIDARG;
57370b324cSopenharmony_ci      if (prop.ulVal > 2)
58370b324cSopenharmony_ci        return E_NOTIMPL;
59370b324cSopenharmony_ci      algo = (unsigned)prop.ulVal;
60370b324cSopenharmony_ci    }
61370b324cSopenharmony_ci  }
62370b324cSopenharmony_ci  if (!Sha256_SetFunction(Sha(), algo))
63370b324cSopenharmony_ci    return E_NOTIMPL;
64370b324cSopenharmony_ci  return S_OK;
65370b324cSopenharmony_ci}
66370b324cSopenharmony_ci
67370b324cSopenharmony_ciREGISTER_HASHER(CSha256Hasher, 0xA, "SHA256", SHA256_DIGEST_SIZE)
68