aboutsummaryrefslogtreecommitdiff
path: root/CPP/Common/Sha256Reg.cpp
blob: b5689c42d49bcfdc770144079f26aa2819f72645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Sha256Reg.cpp

#include "StdAfx.h"

#include "../../C/Sha256.h"

#include "../Common/MyBuffer2.h"
#include "../Common/MyCom.h"

#include "../7zip/Common/RegisterCodec.h"

Z7_CLASS_IMP_COM_2(
  CSha256Hasher
  , IHasher
  , ICompressSetCoderProperties
)
  CAlignedBuffer1 _buf;
public:
  Byte _mtDummy[1 << 7];

  CSha256 *Sha() { return (CSha256 *)(void *)(Byte *)_buf; }
public:
  CSha256Hasher():
    _buf(sizeof(CSha256))
  {
    Sha256_SetFunction(Sha(), 0);
    Sha256_InitState(Sha());
  }
};

Z7_COM7F_IMF2(void, CSha256Hasher::Init())
{
  Sha256_InitState(Sha());
}

Z7_COM7F_IMF2(void, CSha256Hasher::Update(const void *data, UInt32 size))
{
  Sha256_Update(Sha(), (const Byte *)data, size);
}

Z7_COM7F_IMF2(void, CSha256Hasher::Final(Byte *digest))
{
  Sha256_Final(Sha(), digest);
}


Z7_COM7F_IMF(CSha256Hasher::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps))
{
  unsigned algo = 0;
  for (UInt32 i = 0; i < numProps; i++)
  {
    if (propIDs[i] == NCoderPropID::kDefaultProp)
    {
      const PROPVARIANT &prop = coderProps[i];
      if (prop.vt != VT_UI4)
        return E_INVALIDARG;
      if (prop.ulVal > 2)
        return E_NOTIMPL;
      algo = (unsigned)prop.ulVal;
    }
  }
  if (!Sha256_SetFunction(Sha(), algo))
    return E_NOTIMPL;
  return S_OK;
}

REGISTER_HASHER(CSha256Hasher, 0xA, "SHA256", SHA256_DIGEST_SIZE)