#include <SHA256/SHA256.hpp>
#include <cstring>
#include <iomanip>
#include <sstream>

#define RIGHTROTATE(a, n)		(((a) >> (n)) | ((a) << (32-(n))))

bool SHA256::Digest::operator==(const SHA256::Digest &other) const
{
    return memcmp(bytes, other.bytes, sizeof(bytes));
}

void SHA256::uint64_to_be(uint64_t value, uint8_t *buf)
{
    buf[0] = (value >> 56);
    buf[1] = (value >> 48);
    buf[2] = (value >> 40);
    buf[3] = (value >> 32);
    buf[4] = (value >> 24);
    buf[5] = (value >> 16);
    buf[6] = (value >> 8);
    buf[7] = (value >> 0);
}

void SHA256::uint32_to_be(uint32_t value, uint8_t *buf)
{
    buf[0] = (value >> 24);
    buf[1] = (value >> 16);
    buf[2] = (value >> 8);
    buf[3] = (value >> 0);
}

SHA256::SHA256()
{
    m_h[0] = 0x6a09e667;
    m_h[1] = 0xbb67ae85;
    m_h[2] = 0x3c6ef372;
    m_h[3] = 0xa54ff53a;
    m_h[4] = 0x510e527f;
    m_h[5] = 0x9b05688c;
    m_h[6] = 0x1f83d9ab;
    m_h[7] = 0x5be0cd19;
    
    m_len = 0;
    m_buflen = 0;
}

void SHA256::feed(const void *data, size_t size)
{
    static const uint32_t k[64] = {
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
        0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
        0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
        0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
        0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
        0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
    };
    
    const uint8_t *fetch = (const uint8_t*) data;
    while (size--)
    {
        m_len++;
        m_buffer[m_buflen++] = *fetch++;
        
        if (m_buflen == 64)
        {
            m_buflen = 0;
            
            // we have a new chunk, we can do a round
            uint32_t w[64];
            
            int i;
            for (i=0; i<16; i++)
            {
                w[i] = ((uint32_t) m_buffer[(i << 2)] << 24)
                    | ((uint32_t) m_buffer[(i << 2) | 1] << 16)
                    | ((uint32_t) m_buffer[(i << 2) | 2] << 8)
                    | ((uint32_t) m_buffer[(i << 2) | 3]);
            }
            
            for (i=16; i<64; i++)
            {
                uint32_t s0 = RIGHTROTATE(w[i-15], 7) ^ RIGHTROTATE(w[i-15], 18) ^ (w[i-15] >> 3);
                uint32_t s1 = RIGHTROTATE(w[i-2], 17) ^ RIGHTROTATE(w[i-2], 19) ^ (w[i-2] >> 10);
                w[i] = w[i-16] + s0 + w[i-7] + s1;
            }
            
            uint32_t a = m_h[0];
            uint32_t b = m_h[1];
            uint32_t c = m_h[2];
            uint32_t d = m_h[3];
            uint32_t e = m_h[4];
            uint32_t f = m_h[5];
            uint32_t g = m_h[6];
            uint32_t h = m_h[7];
            
            for (i=0; i<64; i++)
            {
                uint32_t S1 = RIGHTROTATE(e, 6) ^ RIGHTROTATE(e, 11) ^ RIGHTROTATE(e, 25);
                uint32_t ch = (e & f) ^ ((~e) & g);
                uint32_t temp1 = h + S1 + ch + k[i] + w[i];
                uint32_t S0 = RIGHTROTATE(a, 2) ^ RIGHTROTATE(a, 13) ^ RIGHTROTATE(a, 22);
                uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
                uint32_t temp2 = S0 + maj;
                
                h = g;
                g = f;
                f = e;
                e = d + temp1;
                d = c;
                c = b;
                b = a;
                a = temp1 + temp2;
            }
            
            m_h[0] += a;
            m_h[1] += b;
            m_h[2] += c;
            m_h[3] += d;
            m_h[4] += e;
            m_h[5] += f;
            m_h[6] += g;
            m_h[7] += h;
        }
    }
}

void SHA256::digest(SHA256::Digest &out)
{
    // append a 1, followed by as many zeroes as necessary until we only have space for 8
    // bytes in the buffer.
    uint64_t bitlen = m_len << 3;
    feed("\x80", 1);
    while (m_buflen != 64-8) feed("", 1);
    
    // convert the bit length to big endian and append
    uint8_t bitbuf[8];
    uint64_to_be(bitlen, bitbuf);
    feed(bitbuf, 8);
    
    // the feed above performed the final round, we have a result
    uint8_t *put = out.bytes;
    int i;
    for (i=0; i<8; i++)
    {
        uint32_to_be(m_h[i], &put[i << 2]);
    }
}

std::ostream& operator<<(std::ostream &os, SHA256::Digest digest)
{
    std::stringstream ss;
    ss << std::hex << std::setfill('0');

    for (uint8_t byte : digest.bytes)
    {
        ss << std::setw(2) << (int) byte;
    }

    return os << ss.str();
}