Source file: /~heha/hsn/esptool.zip/sha.h

/* sha.h - TinyCrypt interface to a SHA-256 implementation
 * 2017 Intel Corporation
 *  Usage:	1) call Sha256::init() to initialize the object.
 *
 *		2) call Sha256::update() to hash the next data segment;
 *		it can be called as many times as needed to hash
 *		all of the segments of data. Order is important.
 *
 *		3) call Sha256::final() to emit the digest
 *		from a hashing operation.
 * 2024 heha:
 * For this implementation, "unsigned" must have exactly 32 bit.
 */
#pragma once
#include "types.h"
#include "intrin.h"

// An array easily accessed byte-wise and uint32-wise
template<size_t SZ,class TT=uint32>struct uvector{
 union{
  TT u[SZ];
  byte b[SZ*sizeof(TT)];
 };
 bool operator==(const uvector<SZ,TT>&rhs) const {return !memcmp(this,&rhs,sizeof*this);}
 operator const TT*() const {return u;}
 operator TT*() {return u;}
};

// Basisklasse: Alle Implementierungen verwenden einen 64-Byte-Puffer zum Befüllen
class Sha{
protected:
 uvector<16>Y;	// Arbeitsblock für MD5 und SHA256
 uint32 Yfill;	// Füllstand von Y in Bytes (0..64)
 uint32 Ycnt;	// Anzahl verarbeiteter Y-Blöcke, <uint32> bis 256 GByte
public:
 void init();	// löscht Zähler
 void update(const void*data, size_t dlen);
 void padlen(bool bswap);
 virtual void transform()=0;	// implementierungsspezifisch
};

// Zwischenklasse mit variabler Hash-Länge
template<size_t N> class ShaN:public Sha{
protected:
 uvector<N>X;
public:
 struct Digest:uvector<N>{
  String<8*N+1>toString() const;
  void fromData(const void*data) {*this=*reinterpret_cast<const uvector<N>*>(data);}
  bool fromString(const char*s);
  Digest() {}	// Standardkonstruktor belassen
  Digest(const uvector<N>&o):uvector<N>(o) {}
 };
};

// Implementierung für MD5
class Md5:public ShaN<4>{
 void transform() override;
public:
 Md5() {init();}
 void init();		// löscht Zähler und setzt X auf Startwert
 Digest final();	// finalisiert Y und greift Hashwert ab
 bool selftest();
};

// Implementierung für SHA-256
class Sha256:public ShaN<8>{
 void transform() override;
public:
 Sha256() {init();}
 void init();		// löscht Zähler und setzt X auf Startwert
 Digest final();	// finalisiert Y und greift Hashwert ab
 bool selftest();
};
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded