GIT repositories

Index page of all the GIT repositories that are clonable form this server via HTTPS. Übersichtsseite aller GIT-Repositories, die von diesem Server aus über git clone (HTTPS) erreichbar sind.

Services

A bunch of service scripts to convert, analyse and generate data. Ein paar Services zum Konvertieren, Analysieren und Generieren von Daten.

GNU octave web interface

A web interface for GNU Octave, which allows to run scientific calculations from netbooks, tables or smartphones. The interface provides a web form generator for Octave script parameters with pre-validation, automatic script list generation, as well presenting of output text, figures and files in a output HTML page. Ein Webinterface für GNU-Octave, mit dem wissenschaftliche Berechnungen von Netbooks, Tablets oder Smartphones aus durchgeführt werden können. Die Schnittstelle beinhaltet einen Formulargenerator für Octave-Scriptparameter, mit Einheiten und Einfabevalidierung. Textausgabe, Abbildungen und generierte Dateien werden abgefangen und in einer HTML-Seite dem Nutzer als Ergebnis zur Verfügung gestellt.

C++ Hash Klassentemplates: CRC, SHA1, SHA512, MD5

C++ hashing class templates: CRC, SHA1, SHA512, MD5

Ein paar wieder ausgegrabene Klassen für Checksummenberechnungen. Vor der Veröffentlichung habe ich sie noch in Templates ungeformt, die Schnittstellen vereinheitlicht.

Some hashing classes I digged out. Before publishing them here I did "templatise" them for convenience and homogenised the interfaces.

Dateien

Files

crc.hh md5.hh sha1.hh sha512.hh

Beispiele

Examples

#include <sw/hash/crc.hh>
#include <sw/hash/md5.hh>
#include <sw/hash/sha1.hh>
#include <sw/hash/sha512.hh>
#include <iostream>
#include <sstream>
 
using namespace std;
 
int main(int argc, const char* argv[])
{
  // Any kind of data example data types
  struct {
    char bytes[100];
    int something;
    unsigned long whatever;
  } data;
 
  string path = "/home/me/a-file";
 
  /////////////////////////////////////////////////////////////
  // SHA1, SHA512 and MD5 static functions return the checksum
  // as hex string.
 
  // String
  cout << sw::sha1::calculate("SHA of std::string") << endl;
 
  // Binary data (void*, size_t)
  cout << sw::sha1::calculate(&data, sizeof(data)) << endl;
 
  // File checksum
  cout << sw::sha1::file(path) << endl;
 
  // Streams (std::istream &) aer possible, too
  std::stringstream ss("SHA of std::stringstream");
  cout << sw::sha1::calculate(ss) << endl;
 
  // The same with md5 and sha512
  cout << sw::md5::calculate("MD5 of std::string") << endl;
  cout << sw::sha512::calculate("SHA512 of std::string") << endl;
  cout << sw::sha512::file(path) << endl;
  cout << sw::sha512::calculate(&data, sizeof(data)) << endl;
  // etc, etc.
 
  /////////////////////////////////////////////////////////////
  // CRC returns a number, not a hex string
  // crc16: uint16_t, crc32: uint32_t
  // `stream` function and `file` function are not exported.
  cout << sw::crc16::calculate("CRC16 of std::string") << endl;
  cout << sw::crc32::calculate("CRC32 of std::string") << endl;
  cout << sw::crc16::calculate(&data, sizeof(data)) << endl;
  cout << sw::crc32::calculate(&data, sizeof(data)) << endl;
  return 0;
}

Quelltexte

Source codes

/**
 * @package de.atwillys.cc.swl
 * @license BSD (simplified)
 * @author Stefan Wilhelm (stfwi)
 *
 * @file crc.hh
 * @ccflags
 * @ldflags
 * @platform linux, bsd, windows
 * @standard >= c++98
 *
 * -----------------------------------------------------------------------------
 *
 * CRC16/32 calculation class template. Not much to say, usage:
 *
 *  uint16_t checksum = sw::crc16::calculate(pointer_to_data, size_of_data);
 *
 *  uint32_t checksum = sw::crc32::calculate(pointer_to_data, size_of_data);
 *
 * -----------------------------------------------------------------------------
 * +++ BSD license header +++
 * Copyright (c) 2008-2014, Stefan Wilhelm (stfwi, <cerbero s@atwilly s.de>)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met: (1) Redistributions
 * of source code must retain the above copyright notice, this list of conditions
 * and the following disclaimer. (2) Redistributions in binary form must reproduce
 * the above copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the distribution.
 * (3) Neither the name of atwillys.de nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
 * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * -----------------------------------------------------------------------------
 */
#ifndef SW_CRC_HH
#define SW_CRC_HH
 
#include <string>
#include <cstring>
#if defined(OS_WIN) || defined (_WINDOWS_) || defined(_WIN32) || defined(__MSC_VER)
#include <stdint.h>
#else
#include <inttypes.h>
#endif
 
namespace sw { namespace detail {
 
/**
 * Type selective lookup tables
 *//**
 * Template class basic_crc
 */
template <typename acc_type, typename size_type, acc_type initial_crc_value, acc_type final_xor_value>
class basic_crc
{
public:
 
  /**
   * Calculate CRC16, std::string
   * @param const std::string & s
   * @return acc_type
   */
  static inline acc_type calculate(const std::string & s)
  { return calculate(s.c_str(), s.length()); }
 
  /**
   * Calculate CRC16, C string
   * @param const char* c_str
   * @return acc_type
   */
  static inline acc_type calculate(const char* c_str)
  { return (!c_str) ? 0 : (calculate(c_str, strlen(c_str))); }
 
  /**
   * Calculate CRC16, raw data and length
   * @param const void *data
   * @param size_type size
   * @return acc_type
   */
  static acc_type calculate(const void *data, register size_type size)
  {
    // Compiler is gently asked to as much as possible in registers, but depending
    // on the processor it will not to that.
    acc_type crc = initial_crc_value;
    const unsigned char *p = (const unsigned char*) data;
    if(data) {
      while(size--) crc = (crc_lookups<acc_type>::tab[((crc) ^ (*p++)) & 0xff] ^ ((crc) >> 8));
    }
    return crc ^ final_xor_value;
  }
};
}}
 
namespace sw {
  typedef detail::basic_crc<uint16_t, size_t, 0xffff     , 0xffff> crc16;
  typedef detail::basic_crc<uint32_t, size_t, 0xffffffff , 0xffffffff> crc32;
}
 
#endif
/**
 * MD5 calculation class template.
 *
 * @file md5.hh
 * @ccflags
 * @ldflags
 * @platform linux, bsd, windows
 * @standard >= c++98
 *
 */
#ifndef MD5_HH
#define MD5_HH
 
#if defined(OS_WIN) || defined (_WINDOWS_) || defined(_WIN32) || defined(__MSC_VER)
#include <stdint.h>
#else
#include <inttypes.h>
#endif
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cstdio>
 
namespace sw { namespace detail {
 
/**
 * @class basic_md5
 * @template
 */
template <typename Char_Type=char>
class basic_md5
{
public:
 
  /**
   * Types
   */
  typedef std::basic_string<Char_Type> str_t;
 
public:
 
  /**
   * Constructor
   */
  inline basic_md5()
  { clear(); }
 
  /**
   * Destructor
   */
  virtual ~basic_md5()
  { ; }
 
public:
 
  /**
   * Clear/reset all internal buffers and states.
   */
  void clear()
  {
    cnt_[0] = cnt_[1] = 0;
    sum_[0] = 0x67452301; sum_[1] = 0xefcdab89; sum_[2] = 0x98badcfe; sum_[3] = 0x10325476;
    memset(buf_, 0, sizeof buf_);
  }
 
  /**
   * Push new binary data into the internal buf_ and recalculate the checksum.
   * @param const void* data
   * @param size_t size
   */
  void update(const void *data, uint32_t size)
  {
    uint32_t index = cnt_[0] / 8 % 64;
    if((cnt_[0] += (size << 3)) < (size << 3)) cnt_[1]++; // Update number of bits
    cnt_[1] += (size >> 29);
    uint32_t i = 0, thresh = 64-index; // number of bytes to fill in buffer
    if(size >= thresh) { // transform as many times as possible.
      memcpy(&buf_[index], data, thresh); // fill buffer first, transform
      transform(buf_);
      for(i=thresh; i+64 <= size; i+=64) transform(((const uint8_t*)data)+i);
      index = 0;
    }
    memcpy(&buf_[index], ((const uint8_t*)data)+i, size-i); // remainder
  }
 
  /**
   * Finanlise checksum, return hex string.
   * @return str_t
   */
  std::string final()
  {
    #define U32_B(O_, I_, len) { \
      for (uint32_t i = 0, j = 0; j < len; i++, j += 4) { \
        (O_)[j] = (I_)[i] & 0xff; \
        (O_)[j+1] = ((I_)[i] >> 8) & 0xff; \
        (O_)[j+2] = ((I_)[i] >> 16) & 0xff; \
        (O_)[j+3] = ((I_)[i] >> 24) & 0xff; \
      } \
    }
 
    uint8_t padding[64];
    memset(padding, 0, sizeof(padding));
    padding[0] = 0x80;
    uint8_t bits[8]; // Save number of bits
    U32_B(bits, cnt_, 8);
    uint32_t index = cnt_[0] / 8 % 64; // pad out to 56 mod 64.
    uint32_t padLen = (index < 56) ? (56 - index) : (120 - index);
    update(padding, padLen);
    update(bits, 8); // Append length (before padding)
    uint8_t res[16];
    U32_B(res, sum_, 16); // Store state in digest
    std::basic_stringstream<Char_Type> ss; // hex string
    for (unsigned i = 0; i < 16; ++i) { // stream hex includes endian conversion
      ss << std::hex << std::setfill('0') << std::setw(2) << (res[i] & 0xff);
    }
    clear();
    return ss.str();
    #undef U32_B
  }
 
public:
 
  /**
   * Calculates the MD5 for a given string.
   * @param const str_t & s
   * @return str_t
   */
  static str_t calculate(const str_t & s)
  { basic_md5 r; r.update(s.data(), s.length()); return r.final(); }
 
  /**
   * Calculates the MD5 for a given C-string.
   * @param const char* s
   * @return str_t
   */
  static str_t calculate(const void* data, size_t size)
  { basic_md5 r; r.update(data, size); return r.final(); }
 
  /**
   * Calculates the MD5 for a stream. Returns an empty string on error.
   * @param std::istream & is
   * @return str_t
   */
  static str_t calculate(std::istream & is)
  {
    basic_md5 r;
    char data[64];
    while(is.good() && is.read(data, sizeof(data)).good()) {
      r.update(data, sizeof(data));
    }
    if(!is.eof()) return str_t();
    if(is.gcount()) r.update(data, is.gcount());
    return r.final();
  }
 
  /**
   * Calculates the MD5 checksum for a given file, either read binary or as text.
   * @param const str_t & path
   * @param bool binary = true
   * @return str_t
   */
  static str_t file(const str_t & path, bool binary=true)
  {
    std::ifstream fs;
    fs.open(path.c_str(), binary ? (std::ios::in|std::ios::binary) : (std::ios::in));
    str_t s = calculate(fs);
    fs.close();
    return s;
  }
 
private:
 
  /**
   * Performs the MD5 transformation on a given block
   * @param uint32_t *block
   */
  void transform(const uint8_t* block)
  {
    #define F1(x,y,z) (((x)&(y)) | (~(x)&(z)))
    #define F2(x,y,z) (((x)&(z)) | ((y)&(~(z))))
    #define F3(x,y,z) ((x)^(y)^(z))
    #define F4(x,y,z) ((y)^((x)|(~(z))))
    #define RL(x,n) (((x)<<(n))|((x)>>(32-(n))))
    #define FF(a,b,c,d,x,s,ac) { a = RL(a+ F1(b,c,d) + (x) + (ac), (s)) + (b); }
    #define GG(a,b,c,d,x,s,ac) { a = RL(a + F2(b,c,d) + (x) + (ac), (s)) + (b); }
    #define HH(a,b,c,d,x,s,ac) { a = RL(a + F3(b,c,d) + x + ac, s) + b; }
    #define II(a,b,c,d,x,s,ac) { a = RL(a + F4(b,c,d) + x + ac, s) + b; }
 
    #define B_U32(output, input, len) { \
      for(unsigned i = 0, j = 0; j < len; i++, j += 4) { \
        (output)[i] = ((uint32_t)(input)[j]) | (((uint32_t)(input)[j+1]) << 8) | \
        (((uint32_t)(input)[j+2]) << 16) | (((uint32_t)(input)[j+3]) << 24); \
      } \
    }
 
    uint32_t a = sum_[0], b = sum_[1], c = sum_[2], d = sum_[3], x[16];
    B_U32 (x, block, 64);
    FF(a,b,c,d,x[0],7,0xd76aa478);   FF(d,a,b,c,x[1],12,0xe8c7b756);
    FF(c,d,a,b,x[2],17,0x242070db);  FF(b,c,d,a,x[3],22,0xc1bdceee);
    FF(a,b,c,d,x[4],7,0xf57c0faf);   FF(d,a,b,c,x[5],12,0x4787c62a);
    FF(c,d,a,b,x[6],17,0xa8304613);  FF(b,c,d,a,x[7],22,0xfd469501);
    FF(a,b,c,d,x[8],7,0x698098d8);   FF(d,a,b,c,x[9],12,0x8b44f7af);
    FF(c,d,a,b,x[10],17,0xffff5bb1); FF(b,c,d,a,x[11],22,0x895cd7be);
    FF(a,b,c,d,x[12],7,0x6b901122);  FF(d,a,b,c,x[13],12,0xfd987193);
    FF(c,d,a,b,x[14],17,0xa679438e); FF(b,c,d,a,x[15],22,0x49b40821);
    GG(a,b,c,d,x[1],5,0xf61e2562);   GG(d,a,b,c,x[6],9,0xc040b340);
    GG(c,d,a,b,x[11],14,0x265e5a51); GG(b,c,d,a,x[0],20,0xe9b6c7aa);
    GG(a,b,c,d,x[5],5,0xd62f105d);   GG(d,a,b,c,x[10],9,0x2441453);
    GG(c,d,a,b,x[15],14,0xd8a1e681); GG(b,c,d,a,x[4],20,0xe7d3fbc8);
    GG(a,b,c,d,x[9],5,0x21e1cde6);   GG(d,a,b,c,x[14],9,0xc33707d6);
    GG(c,d,a,b,x[3],14,0xf4d50d87);  GG(b,c,d,a,x[8],20,0x455a14ed);
    GG(a,b,c,d,x[13],5,0xa9e3e905);  GG(d,a,b,c,x[2],9,0xfcefa3f8);
    GG(c,d,a,b,x[7],14,0x676f02d9);  GG(b,c,d,a,x[12],20,0x8d2a4c8a);
    HH(a,b,c,d,x[5],4,0xfffa3942);   HH(d,a,b,c,x[8],11,0x8771f681);
    HH(c,d,a,b,x[11],16,0x6d9d6122); HH(b,c,d,a,x[14],23,0xfde5380c);
    HH(a,b,c,d,x[1],4,0xa4beea44);   HH(d,a,b,c,x[4],11,0x4bdecfa9);
    HH(c,d,a,b,x[7],16,0xf6bb4b60);  HH(b,c,d,a,x[10],23,0xbebfbc70);
    HH(a,b,c,d,x[13],4,0x289b7ec6);  HH(d,a,b,c,x[0],11,0xeaa127fa);
    HH(c,d,a,b,x[3],16,0xd4ef3085);  HH(b,c,d,a,x[6],23,0x4881d05);
    HH(a,b,c,d,x[9],4,0xd9d4d039);   HH(d,a,b,c,x[12],11,0xe6db99e5);
    HH(c,d,a,b,x[15],16,0x1fa27cf8); HH(b,c,d,a,x[2],23,0xc4ac5665);
    II(a,b,c,d,x[0],6,0xf4292244);   II(d,a,b,c,x[7],10,0x432aff97);
    II(c,d,a,b,x[14],15,0xab9423a7); II(b,c,d,a,x[5],21,0xfc93a039);
    II(a,b,c,d,x[12],6,0x655b59c3);  II(d,a,b,c,x[3],10,0x8f0ccc92);
    II(c,d,a,b,x[10],15,0xffeff47d); II(b,c,d,a,x[1],21,0x85845dd1);
    II(a,b,c,d,x[8],6,0x6fa87e4f);   II(d,a,b,c,x[15],10,0xfe2ce6e0);
    II(c,d,a,b,x[6],15,0xa3014314);  II(b,c,d,a,x[13],21,0x4e0811a1);
    II(a,b,c,d,x[4],6,0xf7537e82);   II(d,a,b,c,x[11],10,0xbd3af235);
    II(c,d,a,b,x[2],15,0x2ad7d2bb);  II(b,c,d,a,x[9],21,0xeb86d391);
    sum_[0] += a; sum_[1] += b; sum_[2] += c; sum_[3] += d;
    memset(x, 0, sizeof x);
    #undef F1
    #undef F2
    #undef F3
    #undef F4
    #undef RL
    #undef FF
    #undef GG
    #undef HH
    #undef II
  }
 
private:
 
  uint8_t buf_[64];
  uint32_t cnt_[2];
  uint32_t sum_[4];
};
}}
 
namespace sw {
  typedef detail::basic_md5<> md5;
}
#endif
/**
 * SHA1 calculation class template.
 *
 * @license %, public domain
 * @author Steve Reid <steve@edmweb.com> (original C source)
 * @author Volker Grabsch <vog@notjusthosting.com> (Small changes to fit into bglibs)
 * @author Bruce Guenter <bruce@untroubled.org> (Translation to simpler C++ Code)
 * @author Stefan Wilhelm <cerbero s@atwillys.de> (class template rewrite, types, endianess)
 *
 * @file sha1.hh
 * @ccflags
 * @ldflags
 * @platform linux, bsd, windows
 * @standard >= c++98
 *
 */
#ifndef SHA1_HH
#define SHA1_HH
 
#if defined(OS_WIN) || defined (_WINDOWS_) || defined(_WIN32) || defined(__MSC_VER)
#include <stdint.h>
#else
#include <inttypes.h>
#endif
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
 
namespace sw { namespace detail {
 
/**
 * @class basic_sha1
 * @template
 */
template <typename Char_Type=char>
class basic_sha1
{
public:
 
  /**
   * Types
   */
  typedef std::basic_string<Char_Type> str_t;
 
public:
 
  /**
   * Constructor
   */
  inline basic_sha1()
  { buf_.reserve(64); clear(); }
 
  /**
   * Destructor
   */
  virtual ~basic_sha1()
  { ; }
 
public:
 
  /**
   * Clear/reset all internal buffers and states.
   */
  void clear()
  {
    sum_[0] = 0x67452301; sum_[1] = 0xefcdab89; sum_[2] = 0x98badcfe; sum_[3] = 0x10325476;
    sum_[4] = 0xc3d2e1f0; iterations_ = 0; buf_.clear();
  }
 
  /**
   * Push new binary data into the internal buf_ and recalculate the checksum.
   * @param const void* data
   * @param size_t size
   */
  void update(const void* data, size_t size)
  {
    if(!data || !size) return;
    const char* p = (const char*) data;
    uint32_t block[16];
    if(!buf_.empty()) { // Deal with the remaining buf_ data
      while(size && buf_.length() < 64) { buf_ += *p++; --size; } // Copy bytes
      if(buf_.length() < 64) return; // Not enough data
      const char* pp = (const char*) buf_.data();
      for(unsigned i = 0; i < 16; ++i) {
        #if (defined (BYTE_ORDER)) && (defined (BIG_ENDIAN)) && ((BYTE_ORDER == BIG_ENDIAN))
        block[i] = (pp[0] << 0) | (pp[1] << 8) | (pp[2] << 16) | (pp[3] << 24);
        #else
        block[i] = (pp[3] << 0) | (pp[2] << 8) | (pp[1] << 16) | (pp[0] << 24);
        #endif
        pp += 4;
      }
      buf_.clear();
      transform(block);
    }
    while(size >= 64) { // Transform full blocks
      for(unsigned i = 0; i < 16; ++i) {
        #if (defined (BYTE_ORDER)) && (defined (BIG_ENDIAN)) && ((BYTE_ORDER == BIG_ENDIAN))
        block[i] = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
        #else
        block[i] = (p[3] << 0) | (p[2] << 8) | (p[1] << 16) | (p[0] << 24);
        #endif
        p += 4;
      }
      transform(block);
      size -= 64;
    }
    while(size--) {
      buf_ += *p++; // Transfer remaining bytes into the buf_
    }
  }
 
  /**
   * Finanlise checksum, return hex string.
   * @return str_t
   */
  str_t final()
  {
    uint64_t total_bits = (iterations_ * 64 + buf_.size()) * 8;
    buf_ += (char) 0x80;
    typename std::string::size_type sz = buf_.size();
    while (buf_.size() < 64) buf_ += (char) 0;
    uint32_t block[16];
    for(unsigned i = 0; i < 16; i++) {
      #if (defined (BYTE_ORDER)) && (defined (BIG_ENDIAN)) && ((BYTE_ORDER == BIG_ENDIAN))
      block[i] = ((buf_[4*i+0] & 0xff) << 0) | ((buf_[4*i+1] & 0xff) << 8) |
         ((buf_[4*i+2] & 0xff) << 16) | ((buf_[4*i+3] & 0xff) << 24);
      #else
      block[i] = ((buf_[4*i+3] & 0xff) << 0) | ((buf_[4*i+2] & 0xff) << 8) |
         ((buf_[4*i+1] & 0xff) << 16) | ((buf_[4*i+0] & 0xff) << 24);
      #endif
    }
    if(sz > 56) {
      transform(block);
      for(unsigned i=0; i<14; ++i) block[i] = 0;
    }
    block[15] = (total_bits >>  0);
    block[14] = (total_bits >> 32);
    transform(block);
    std::basic_stringstream<Char_Type> ss; // hex string
    for (unsigned i = 0; i < 5; ++i) { // stream hex includes endian conversion
      ss << std::hex << std::setfill('0') << std::setw(8) << (sum_[i] & 0xffffffff);
    }
    clear();
    return ss.str();
  }
 
public:
 
  /**
   * Calculates the SHA1 for a given string.
   * @param const str_t & s
   * @return str_t
   */
  static str_t calculate(const str_t & s)
  { basic_sha1 r; r.update(s.data(), s.length()); return r.final(); }
 
  /**
   * Calculates the SHA1 for a given C-string.
   * @param const char* s
   * @return str_t
   */
  static str_t calculate(const void* data, size_t size)
  { basic_sha1 r; r.update(data, size); return r.final(); }
 
  /**
   * Calculates the SHA1 for a stream. Returns an empty string on error.
   * @param std::istream & is
   * @return str_t
   */
  static str_t calculate(std::istream & is)
  {
    basic_sha1 r;
    char data[64];
    while(is.good() && is.read(data, sizeof(data)).good()) {
      r.update(data, sizeof(data));
    }
    if(!is.eof()) return str_t();
    if(is.gcount()) r.update(data, is.gcount());
    return r.final();
  }
 
  /**
   * Calculates the SHA1 checksum for a given file, either read binary or as text.
   * @param const str_t & path
   * @param bool binary = true
   * @return str_t
   */
  static str_t file(const str_t & path, bool binary=true)
  {
    std::ifstream fs;
    fs.open(path.c_str(), binary ? (std::ios::in|std::ios::binary) : (std::ios::in));
    str_t s = calculate(fs);
    fs.close();
    return s;
  }
 
private:
 
  /**
   * Performs the SHA1 transformation on a given block
   * @param uint32_t *block
   */
  void transform(uint32_t *block)
  {
    #define rol(value, bits) (((value) << (bits)) | (((value) & 0xffffffff) >> (32-(bits))))
    #define blk(i) (block[i&15]=rol(block[(i+13)&15]^block[(i+8)&15]^block[(i+2)&15]^block[i&15],1))
    #define R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v,5); w=rol(w,30);
    #define R1(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk(i) + 0x5a827999 + rol(v,5); w=rol(w,30);
    #define R2(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0x6ed9eba1 + rol(v,5); w=rol(w,30);
    #define R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + blk(i) + 0x8f1bbcdc + rol(v,5); w=rol(w,30);
    #define R4(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0xca62c1d6 + rol(v,5); w=rol(w,30);
    uint32_t a = sum_[0], b = sum_[1], c = sum_[2], d = sum_[3], e = sum_[4];
    R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); R0(b,c,d,e,a, 4);
    R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9);
    R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14);
    R0(a,b,c,d,e,15); R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
    R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); R2(b,c,d,e,a,24);
    R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); R2(c,d,e,a,b,28); R2(b,c,d,e,a,29);
    R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34);
    R2(a,b,c,d,e,35); R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
    R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); R3(b,c,d,e,a,44);
    R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); R3(c,d,e,a,b,48); R3(b,c,d,e,a,49);
    R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54);
    R3(a,b,c,d,e,55); R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
    R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); R4(b,c,d,e,a,64);
    R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); R4(c,d,e,a,b,68); R4(b,c,d,e,a,69);
    R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74);
    R4(a,b,c,d,e,75); R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
    sum_[0] += a; sum_[1] += b; sum_[2] += c; sum_[3] += d; sum_[4] += e; iterations_++;
    #undef rol
    #undef blk
    #undef R0
    #undef R1
    #undef R2
    #undef R3
    #undef R4
  }
 
private:
 
  uint64_t iterations_; // Number of iterations
  uint32_t sum_[5];     // Intermediate checksum digest buffer
  std::string buf_;     // Intermediate buffer for remaining pushed data
};
}}
 
namespace sw {
  typedef detail::basic_sha1<> sha1;
}
 
#endif
/**
 * @file sha512.hh
 * @author Stefan Wilhelm (stfwi)
 * @ccflags
 * @ldflags
 * @platform linux, bsd, windows
 * @standard >= c++98
 *
 * SHA512 calculation class template.
 *
 * -------------------------------------------------------------------------------------
 * +++ BSD license header +++
 * Copyright (c) 2010, 2012, Stefan Wilhelm (stfwi, <cerbero s@atwilly s.de>)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met: (1) Redistributions
 * of source code must retain the above copyright notice, this list of conditions
 * and the following disclaimer. (2) Redistributions in binary form must reproduce
 * the above copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the distribution.
 * (3) Neither the name of the project nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
 * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * -------------------------------------------------------------------------------------
 * 01-2013: (stfwi) Class to template class, reformatting
 */
#ifndef SHA512_HH
#define SHA512_HH
 
#if defined(OS_WIN) || defined (_WINDOWS_) || defined(_WIN32) || defined(__MSC_VER)
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
 
namespace sw { namespace detail {
 
/**
 * @class basic_sha512
 * @template
 */
template <typename Char_Type=char>
class basic_sha512
{
public:
 
  /**
   * Types
   */
  typedef std::basic_string<Char_Type> str_t;
 
public:
 
  /**
   * Constructor
   */
  basic_sha512()
  { clear(); }
 
  /**
   * Destructor
   */
  ~basic_sha512()
  { ; }
 
public:
 
  /**
   * Clear/reset all internal buffers and states.
   */
  void clear()
  {
    sum_[0] = 0x6a09e667f3bcc908; sum_[1] = 0xbb67ae8584caa73b;
    sum_[2] = 0x3c6ef372fe94f82b; sum_[3] = 0xa54ff53a5f1d36f1;
    sum_[4] = 0x510e527fade682d1; sum_[5] = 0x9b05688c2b3e6c1f;
    sum_[6] = 0x1f83d9abfb41bd6b; sum_[7] = 0x5be0cd19137e2179;
    sz_ = 0; iterations_ = 0; memset(&block_, 0, sizeof(block_));
  }
 
  /**
   * Push new binary data into the internal buf_ and recalculate the checksum.
   * @param const void* data
   * @param size_t size
   */
  void update(const void* data, size_t size)
  {
    unsigned nb, n, n_tail;
    const uint8_t *p;
    n = 128 - sz_;
    n_tail = size < n ? size : n;
    memcpy(&block_[sz_], data, n_tail);
    if (sz_ + size < 128) { sz_ += size; return; }
    n = size - n_tail;
    nb = n >> 7;
    p = (const uint8_t*) data + n_tail;
    transform(block_, 1);
    transform(p, nb);
    n_tail = n & 0x7f;
    memcpy(block_, &p[nb << 7], n_tail);
    sz_ = n_tail;
    iterations_ += (nb+1) << 7;
  }
 
  /**
   * Finanlise checksum, return hex string.
   * @return str_t
   */
  str_t final_data()
  {
    #if (defined (BYTE_ORDER)) && (defined (BIG_ENDIAN)) && ((BYTE_ORDER == BIG_ENDIAN))
    #define U32_B(x,b) *((b)+0)=(uint8_t)((x)); *((b)+1)=(uint8_t)((x)>>8); \
            *((b)+2)=(uint8_t)((x)>>16); *((b)+3)=(uint8_t)((x)>>24);
    #else
    #define U32_B(x,b) *((b)+3)=(uint8_t)((x)); *((b)+2)=(uint8_t)((x)>>8); \
            *((b)+1)=(uint8_t)((x)>>16); *((b)+0)=(uint8_t)((x)>>24);
    #endif
    unsigned nb, n;
    uint64_t n_total;
    nb = 1 + ((0x80-17) < (sz_ & 0x7f));
    n_total = (iterations_ + sz_) << 3;
    n = nb << 7;
    memset(block_ + sz_, 0, n - sz_);
    block_[sz_] = 0x80;
    U32_B(n_total, block_ + n-4);
    transform(block_, nb);
    std::basic_stringstream<Char_Type> ss; // hex string
    for (unsigned i = 0; i < 8; ++i) {
      ss << std::hex << std::setfill('0') << std::setw(16) << (sum_[i]);
    }
    clear();
    return ss.str();
    #undef U32_B
  }
 
public:
 
  /**
   * Calculates the SHA256 for a given string.
   * @param const str_t & s
   * @return str_t
   */
  static str_t calculate(const str_t & s)
  {
    basic_sha512 r;
    r.update(s.data(), s.length());
    return r.final_data();
  }
 
  /**
   * Calculates the SHA256 for a given C-string.
   * @param const char* s
   * @return str_t
   */
  static str_t calculate(const void* data, size_t size)
  { basic_sha512 r; r.update(data, size); return r.final_data(); }
 
  /**
   * Calculates the SHA256 for a stream. Returns an empty string on error.
   * @param std::istream & is
   * @return str_t
   */
  static str_t calculate(std::istream & is)
  {
    basic_sha512 r;
    char data[64];
    while(is.good() && is.read(data, sizeof(data)).good()) {
      r.update(data, sizeof(data));
    }
    if(!is.eof()) return str_t();
    if(is.gcount()) r.update(data, is.gcount());
    return r.final_data();
  }
 
  /**
   * Calculates the SHA256 checksum for a given file, either read binary or as text.
   * @param const str_t & path
   * @param bool binary = true
   * @return str_t
   */
  static str_t file(const str_t & path, bool binary=true)
  {
    std::ifstream fs;
    fs.open(path.c_str(), binary ? (std::ios::in|std::ios::binary) : (std::ios::in));
    str_t s = calculate(fs);
    fs.close();
    return s;
  }
 
private:
 
  /**
   * Performs the SHA256 transformation on a given block
   * @param uint32_t *block
   */
  void transform(const uint8_t *data, size_t size)
  {
    #define SR(x, n) (x >> n)
    #define RR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
    #define RL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
    #define CH(x, y, z)  ((x & y) ^ (~x & z))
    #define MJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
    #define F1(x) (RR(x, 28) ^ RR(x, 34) ^ RR(x, 39))
    #define F2(x) (RR(x, 14) ^ RR(x, 18) ^ RR(x, 41))
    #define F3(x) (RR(x,  1) ^ RR(x,  8) ^ SR(x,  7))
    #define F4(x) (RR(x, 19) ^ RR(x, 61) ^ SR(x,  6))
    #if (defined (BYTE_ORDER)) && (defined (BIG_ENDIAN)) && ((BYTE_ORDER == BIG_ENDIAN))
    #define B_U64(b,x) *(x)=((uint64_t)*((b)+0))|((uint64_t)*((b)+1)<<8)|\
      ((uint64_t)*((b)+2)<<16)|((uint64_t)*((b)+3)<<24)|((uint64_t)*((b)+4)<<32)|\
      ((uint64_t)*((b)+5)<<40)|((uint64_t)*((b)+6)<<48)|((uint64_t)*((b)+7)<<56);
    #else
    #define B_U64(b,x) *(x)=((uint64_t)*((b)+7))|((uint64_t)*((b)+6)<<8)|\
      ((uint64_t)*((b)+5)<<16)|((uint64_t)*((b)+4)<<24)|((uint64_t)*((b)+3)<<32)|\
      ((uint64_t)*((b)+2)<<40)|((uint64_t)*((b)+1)<<48)|((uint64_t)*((b)+0)<<56);
    #endif
    uint64_t t, u, v[8], w[80];
    const uint8_t *tblock;
    unsigned j;
    for(unsigned i = 0; i < size; ++i) {
      tblock = data + (i << 7);
      for(j = 0; j < 16; ++j) B_U64(&tblock[j<<3], &w[j]);
      for(j = 16; j < 80; ++j) w[j] = F4(w[j-2]) + w[j-7] + F3(w[j-15]) + w[j-16];
      for(j = 0; j < 8; ++j) v[j] = sum_[j];
      for(j = 0; j < 80; ++j) {
        t = v[7] + F2(v[4]) + CH(v[4], v[5], v[6]) + lut_[j] + w[j];
        u = F1(v[0]) + MJ(v[0], v[1], v[2]); v[7] = v[6]; v[6] = v[5]; v[5] = v[4];
        v[4] = v[3] + t; v[3] = v[2]; v[2] = v[1]; v[1] = v[0]; v[0] = t + u;
      }
      for(j = 0; j < 8; ++j) sum_[j] += v[j];
    }
    #undef SR
    #undef RR
    #undef RL
    #undef CH
    #undef MJ
    #undef F1
    #undef F2
    #undef F3
    #undef F4
    #undef B_U64
  }
 
private:
 
  uint64_t iterations_; // Number of iterations
  uint64_t sum_[8];     // Intermediate checksum buffer
  unsigned sz_;         // Number of currently stored bytes in the block
  uint8_t  block_[256];
  static const uint64_t lut_[80]; // Lookup table
};
 
template <typename CT>
const uint64_t basic_sha512<CT>::lut_[80] = {
  0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
  0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
  0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
  0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
  0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
  0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
  0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
  0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
  0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
  0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
  0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
  0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
  0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
  0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
  0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
  0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
  0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
  0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
  0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
  0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
};
 
}}
 
namespace sw {
  typedef detail::basic_sha512<> sha512;
}
 
#endif