Exemple #1
0
void Sha256_Final(CSha256 *p, Byte *digest)
{
  UInt64 lenInBits = (p->count << 3);
  UInt32 curBufferPos = (UInt32)p->count & 0x3F;
  unsigned i;
  p->buffer[curBufferPos++] = 0x80;
  while (curBufferPos != (64 - 8))
  {
    curBufferPos &= 0x3F;
    if (curBufferPos == 0)
      Sha256_WriteByteBlock(p);
    p->buffer[curBufferPos++] = 0;
  }
  for (i = 0; i < 8; i++)
  {
    p->buffer[curBufferPos++] = (Byte)(lenInBits >> 56);
    lenInBits <<= 8;
  }
  Sha256_WriteByteBlock(p);

  for (i = 0; i < 8; i++)
  {
    *digest++ = (Byte)((p->state[i] >> 24) & 0xFF);
    *digest++ = (Byte)((p->state[i] >> 16) & 0xFF);
    *digest++ = (Byte)((p->state[i] >> 8) & 0xFF);
    *digest++ = (Byte)((p->state[i]) & 0xFF);
  }
  Sha256_Init(p);
}
Exemple #2
0
void Sha256_Final(CSha256 *p, Byte *digest)
{
  unsigned pos = (unsigned)p->count & 0x3F;
  unsigned i;
  
  p->buffer[pos++] = 0x80;
  
  while (pos != (64 - 8))
  {
    pos &= 0x3F;
    if (pos == 0)
      Sha256_WriteByteBlock(p);
    p->buffer[pos++] = 0;
  }

  {
    UInt64 numBits = (p->count << 3);
    SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32));
    SetBe32(p->buffer + 64 - 4, (UInt32)(numBits));
  }
  
  Sha256_WriteByteBlock(p);

  for (i = 0; i < 8; i += 2)
  {
    UInt32 v0 = p->state[i];
    UInt32 v1 = p->state[i + 1];
    SetBe32(digest    , v0);
    SetBe32(digest + 4, v1);
    digest += 8;
  }
  
  Sha256_Init(p);
}
Exemple #3
0
static void Sha256_Final(CSha256 *p, uint8_t *digest)
{
    uint64_t lenInBits = (p->count << 3);
    uint32_t curBufferPos = (uint32_t)p->count & 0x3F;
    unsigned i;
    p->buffer[curBufferPos++] = 0x80;
    while (curBufferPos != (64 - 8))
    {
        curBufferPos &= 0x3F;
        if (curBufferPos == 0)
            Sha256_WriteByteBlock(p);
        p->buffer[curBufferPos++] = 0;
    }
    for (i = 0; i < 8; i++)
    {
        p->buffer[curBufferPos++] = (uint8_t)(lenInBits >> 56);
        lenInBits <<= 8;
    }
    Sha256_WriteByteBlock(p);

    for (i = 0; i < 8; i++)
    {
        *digest++ = (uint8_t)(p->state[i] >> 24);
        *digest++ = (uint8_t)(p->state[i] >> 16);
        *digest++ = (uint8_t)(p->state[i] >> 8);
        *digest++ = (uint8_t)(p->state[i]);
    }
    Sha256_Init(p);
}
Exemple #4
0
void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
{
  UInt32 curBufferPos = (UInt32)p->count & 0x3F;
  while (size > 0)
  {
	  p->buffer[curBufferPos++] = *data++;
	  p->count++;
	  size--;
	  if (curBufferPos == 64)
	  {
		  curBufferPos = 0;
		  Sha256_WriteByteBlock(p);
	  }
  }

  /*
  critical_section ct;

  parallel::_for( 0, size,
	  [ &p, &data, &curBufferPos, &ct ](int i, bool & breakFlag)
  {
	  UInt32 cp = curBufferPos + i;
	  p->buffer[ cp ] = data[i];
	  if( cp )
	  {

	  }
  }
  );
  */
}
Exemple #5
0
std::string compute_sha256(const uint8_t* data, size_t size) {
    CSha256 p;
    p.state[0] = 0x6a09e667;
    p.state[1] = 0xbb67ae85;
    p.state[2] = 0x3c6ef372;
    p.state[3] = 0xa54ff53a;
    p.state[4] = 0x510e527f;
    p.state[5] = 0x9b05688c;
    p.state[6] = 0x1f83d9ab;
    p.state[7] = 0x5be0cd19;
    p.count = 0;
    uint32_t curBufferPos = (uint32_t)p.count & 0x3F;
    while (size) {
        p.buffer[curBufferPos++] = *data++;
        p.count++;
        size--;
        if (curBufferPos == 64) {
            curBufferPos = 0;
            Sha256_WriteByteBlock(&p);
        }
    }
    uint8_t digest[32];
    uint64_t lenInBits = (p.count << 3);
    curBufferPos = (uint32_t)p.count & 0x3F;
    unsigned i;
    p.buffer[curBufferPos++] = 0x80;
    while (curBufferPos != (64 - 8)) {
        curBufferPos &= 0x3F;
        if (curBufferPos == 0)
            Sha256_WriteByteBlock(&p);
        p.buffer[curBufferPos++] = 0;
    }
    for (i = 0; i < 8; i++) {
        p.buffer[curBufferPos++] = (uint8_t)(lenInBits >> 56);
        lenInBits <<= 8;
    }
    Sha256_WriteByteBlock(&p);

    for (i = 0; i < 8; i++) {
        digest[i*4  ] = (uint8_t)(p.state[i] >> 24);
        digest[i*4+1] = (uint8_t)(p.state[i] >> 16);
        digest[i*4+2] = (uint8_t)(p.state[i] >> 8);
        digest[i*4+3] = (uint8_t)(p.state[i]);
    }

    return std::string((const char*) &digest[0], 32);
}
Exemple #6
0
void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
{
  UInt32 curBufferPos = (UInt32)p->count & 0x3F;
  while (size > 0)
  {
    p->buffer[curBufferPos++] = *data++;
    p->count++;
    size--;
    if (curBufferPos == 64)
    {
      curBufferPos = 0;
      Sha256_WriteByteBlock(p);
    }
  }
}
Exemple #7
0
static void Sha256_Update(CSha256 *p, const uint8_t *data, size_t size)
{
    uint32_t curBufferPos = (uint32_t)p->count & 0x3F;
    while (size > 0)
    {
        p->buffer[curBufferPos++] = *data++;
        p->count++;
        size--;
        if (curBufferPos == 64)
        {
            curBufferPos = 0;
            Sha256_WriteByteBlock(p);
        }
    }
}
Exemple #8
0
void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
{
  if (size == 0)
    return;

  {
    unsigned pos = (unsigned)p->count & 0x3F;
    unsigned num;
    
    p->count += size;
    
    num = 64 - pos;
    if (num > size)
    {
      memcpy(p->buffer + pos, data, size);
      return;
    }
    
    size -= num;
    memcpy(p->buffer + pos, data, num);
    data += num;
  }

  for (;;)
  {
    Sha256_WriteByteBlock(p);
    if (size < 64)
      break;
    size -= 64;
    memcpy(p->buffer, data, 64);
    data += 64;
  }

  if (size != 0)
    memcpy(p->buffer, data, size);
}