mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
add license header to some crypto files
This commit is contained in:
parent
be0c1ac03a
commit
4c40651451
|
@ -1,28 +1,25 @@
|
|||
/**
|
||||
* rijndael-alg-fst.c
|
||||
*
|
||||
* @version 3.0 (December 2000)
|
||||
*
|
||||
* Optimised ANSI C code for the Rijndael cipher (now AES)
|
||||
*
|
||||
* @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
|
||||
* @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
|
||||
* @author Paulo Barreto <paulo.barreto@terra.com.br>
|
||||
*
|
||||
* This code is hereby placed in the public domain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''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 AUTHORS 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.
|
||||
*/
|
||||
// Copyright (c) 2017 Thomas Fussell
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE
|
||||
//
|
||||
// @license: http://www.opensource.org/licenses/mit-license.php
|
||||
// @author: see AUTHORS file
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
|
|
|
@ -1,3 +1,26 @@
|
|||
// Copyright (c) 2017 Thomas Fussell
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE
|
||||
//
|
||||
// @license: http://www.opensource.org/licenses/mit-license.php
|
||||
// @author: see AUTHORS file
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
|
|
@ -1,16 +1,34 @@
|
|||
// Copyright (c) 2017 Thomas Fussell
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE
|
||||
//
|
||||
// @license: http://www.opensource.org/licenses/mit-license.php
|
||||
// @author: see AUTHORS file
|
||||
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include "sha.hpp"
|
||||
#include <detail/crypto/sha.hpp>
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wsign-conversion"
|
||||
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
|
||||
|
||||
namespace SHA1 {
|
||||
namespace {
|
||||
|
||||
class SHA1
|
||||
{
|
||||
|
@ -18,8 +36,7 @@ public:
|
|||
SHA1();
|
||||
void update(const std::string &s);
|
||||
void update(std::istream &is);
|
||||
std::string final();
|
||||
static std::string from_file(const std::string &filename);
|
||||
std::string final_();
|
||||
|
||||
private:
|
||||
uint32_t digest[5];
|
||||
|
@ -30,8 +47,7 @@ private:
|
|||
static const size_t BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */
|
||||
static const size_t BLOCK_BYTES = BLOCK_INTS * 4;
|
||||
|
||||
|
||||
static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
|
||||
static void sha1_reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
|
||||
{
|
||||
/* SHA1 initialization constants */
|
||||
digest[0] = 0x67452301;
|
||||
|
@ -46,15 +62,15 @@ static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
|
|||
}
|
||||
|
||||
|
||||
static uint32_t rol(const uint32_t value, const size_t bits)
|
||||
static uint32_t sha1_rol(const uint32_t value, const size_t bits)
|
||||
{
|
||||
return (value << bits) | (value >> (32 - bits));
|
||||
}
|
||||
|
||||
|
||||
static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i)
|
||||
static uint32_t sha1_blk(const uint32_t block[BLOCK_INTS], const size_t i)
|
||||
{
|
||||
return rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1);
|
||||
return sha1_rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -62,50 +78,49 @@ static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i)
|
|||
* (R0+R1), R2, R3, R4 are the different operations used in SHA1
|
||||
*/
|
||||
|
||||
static void R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
static void sha1_R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + sha1_rol(v, 5);
|
||||
w = sha1_rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
static void sha1_R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
block[i] = sha1_blk(block, i);
|
||||
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + sha1_rol(v, 5);
|
||||
w = sha1_rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
static void sha1_R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (w^x^y) + block[i] + 0x6ed9eba1 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
block[i] = sha1_blk(block, i);
|
||||
z += (w^x^y) + block[i] + 0x6ed9eba1 + sha1_rol(v, 5);
|
||||
w = sha1_rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
static void sha1_R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
block[i] = sha1_blk(block, i);
|
||||
z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + sha1_rol(v, 5);
|
||||
w = sha1_rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
static void sha1_R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (w^x^y) + block[i] + 0xca62c1d6 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
block[i] = sha1_blk(block, i);
|
||||
z += (w^x^y) + block[i] + 0xca62c1d6 + sha1_rol(v, 5);
|
||||
w = sha1_rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Hash a single 512-bit block. This is the core of the algorithm.
|
||||
*/
|
||||
|
||||
static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
|
||||
static void sha1_transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
|
||||
{
|
||||
/* Copy digest[] to working vars */
|
||||
uint32_t a = digest[0];
|
||||
|
@ -115,86 +130,86 @@ static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &t
|
|||
uint32_t e = digest[4];
|
||||
|
||||
/* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
R0(block, a, b, c, d, e, 0);
|
||||
R0(block, e, a, b, c, d, 1);
|
||||
R0(block, d, e, a, b, c, 2);
|
||||
R0(block, c, d, e, a, b, 3);
|
||||
R0(block, b, c, d, e, a, 4);
|
||||
R0(block, a, b, c, d, e, 5);
|
||||
R0(block, e, a, b, c, d, 6);
|
||||
R0(block, d, e, a, b, c, 7);
|
||||
R0(block, c, d, e, a, b, 8);
|
||||
R0(block, b, c, d, e, a, 9);
|
||||
R0(block, a, b, c, d, e, 10);
|
||||
R0(block, e, a, b, c, d, 11);
|
||||
R0(block, d, e, a, b, c, 12);
|
||||
R0(block, c, d, e, a, b, 13);
|
||||
R0(block, b, c, d, e, a, 14);
|
||||
R0(block, a, b, c, d, e, 15);
|
||||
R1(block, e, a, b, c, d, 0);
|
||||
R1(block, d, e, a, b, c, 1);
|
||||
R1(block, c, d, e, a, b, 2);
|
||||
R1(block, b, c, d, e, a, 3);
|
||||
R2(block, a, b, c, d, e, 4);
|
||||
R2(block, e, a, b, c, d, 5);
|
||||
R2(block, d, e, a, b, c, 6);
|
||||
R2(block, c, d, e, a, b, 7);
|
||||
R2(block, b, c, d, e, a, 8);
|
||||
R2(block, a, b, c, d, e, 9);
|
||||
R2(block, e, a, b, c, d, 10);
|
||||
R2(block, d, e, a, b, c, 11);
|
||||
R2(block, c, d, e, a, b, 12);
|
||||
R2(block, b, c, d, e, a, 13);
|
||||
R2(block, a, b, c, d, e, 14);
|
||||
R2(block, e, a, b, c, d, 15);
|
||||
R2(block, d, e, a, b, c, 0);
|
||||
R2(block, c, d, e, a, b, 1);
|
||||
R2(block, b, c, d, e, a, 2);
|
||||
R2(block, a, b, c, d, e, 3);
|
||||
R2(block, e, a, b, c, d, 4);
|
||||
R2(block, d, e, a, b, c, 5);
|
||||
R2(block, c, d, e, a, b, 6);
|
||||
R2(block, b, c, d, e, a, 7);
|
||||
R3(block, a, b, c, d, e, 8);
|
||||
R3(block, e, a, b, c, d, 9);
|
||||
R3(block, d, e, a, b, c, 10);
|
||||
R3(block, c, d, e, a, b, 11);
|
||||
R3(block, b, c, d, e, a, 12);
|
||||
R3(block, a, b, c, d, e, 13);
|
||||
R3(block, e, a, b, c, d, 14);
|
||||
R3(block, d, e, a, b, c, 15);
|
||||
R3(block, c, d, e, a, b, 0);
|
||||
R3(block, b, c, d, e, a, 1);
|
||||
R3(block, a, b, c, d, e, 2);
|
||||
R3(block, e, a, b, c, d, 3);
|
||||
R3(block, d, e, a, b, c, 4);
|
||||
R3(block, c, d, e, a, b, 5);
|
||||
R3(block, b, c, d, e, a, 6);
|
||||
R3(block, a, b, c, d, e, 7);
|
||||
R3(block, e, a, b, c, d, 8);
|
||||
R3(block, d, e, a, b, c, 9);
|
||||
R3(block, c, d, e, a, b, 10);
|
||||
R3(block, b, c, d, e, a, 11);
|
||||
R4(block, a, b, c, d, e, 12);
|
||||
R4(block, e, a, b, c, d, 13);
|
||||
R4(block, d, e, a, b, c, 14);
|
||||
R4(block, c, d, e, a, b, 15);
|
||||
R4(block, b, c, d, e, a, 0);
|
||||
R4(block, a, b, c, d, e, 1);
|
||||
R4(block, e, a, b, c, d, 2);
|
||||
R4(block, d, e, a, b, c, 3);
|
||||
R4(block, c, d, e, a, b, 4);
|
||||
R4(block, b, c, d, e, a, 5);
|
||||
R4(block, a, b, c, d, e, 6);
|
||||
R4(block, e, a, b, c, d, 7);
|
||||
R4(block, d, e, a, b, c, 8);
|
||||
R4(block, c, d, e, a, b, 9);
|
||||
R4(block, b, c, d, e, a, 10);
|
||||
R4(block, a, b, c, d, e, 11);
|
||||
R4(block, e, a, b, c, d, 12);
|
||||
R4(block, d, e, a, b, c, 13);
|
||||
R4(block, c, d, e, a, b, 14);
|
||||
R4(block, b, c, d, e, a, 15);
|
||||
sha1_R0(block, a, b, c, d, e, 0);
|
||||
sha1_R0(block, e, a, b, c, d, 1);
|
||||
sha1_R0(block, d, e, a, b, c, 2);
|
||||
sha1_R0(block, c, d, e, a, b, 3);
|
||||
sha1_R0(block, b, c, d, e, a, 4);
|
||||
sha1_R0(block, a, b, c, d, e, 5);
|
||||
sha1_R0(block, e, a, b, c, d, 6);
|
||||
sha1_R0(block, d, e, a, b, c, 7);
|
||||
sha1_R0(block, c, d, e, a, b, 8);
|
||||
sha1_R0(block, b, c, d, e, a, 9);
|
||||
sha1_R0(block, a, b, c, d, e, 10);
|
||||
sha1_R0(block, e, a, b, c, d, 11);
|
||||
sha1_R0(block, d, e, a, b, c, 12);
|
||||
sha1_R0(block, c, d, e, a, b, 13);
|
||||
sha1_R0(block, b, c, d, e, a, 14);
|
||||
sha1_R0(block, a, b, c, d, e, 15);
|
||||
sha1_R1(block, e, a, b, c, d, 0);
|
||||
sha1_R1(block, d, e, a, b, c, 1);
|
||||
sha1_R1(block, c, d, e, a, b, 2);
|
||||
sha1_R1(block, b, c, d, e, a, 3);
|
||||
sha1_R2(block, a, b, c, d, e, 4);
|
||||
sha1_R2(block, e, a, b, c, d, 5);
|
||||
sha1_R2(block, d, e, a, b, c, 6);
|
||||
sha1_R2(block, c, d, e, a, b, 7);
|
||||
sha1_R2(block, b, c, d, e, a, 8);
|
||||
sha1_R2(block, a, b, c, d, e, 9);
|
||||
sha1_R2(block, e, a, b, c, d, 10);
|
||||
sha1_R2(block, d, e, a, b, c, 11);
|
||||
sha1_R2(block, c, d, e, a, b, 12);
|
||||
sha1_R2(block, b, c, d, e, a, 13);
|
||||
sha1_R2(block, a, b, c, d, e, 14);
|
||||
sha1_R2(block, e, a, b, c, d, 15);
|
||||
sha1_R2(block, d, e, a, b, c, 0);
|
||||
sha1_R2(block, c, d, e, a, b, 1);
|
||||
sha1_R2(block, b, c, d, e, a, 2);
|
||||
sha1_R2(block, a, b, c, d, e, 3);
|
||||
sha1_R2(block, e, a, b, c, d, 4);
|
||||
sha1_R2(block, d, e, a, b, c, 5);
|
||||
sha1_R2(block, c, d, e, a, b, 6);
|
||||
sha1_R2(block, b, c, d, e, a, 7);
|
||||
sha1_R3(block, a, b, c, d, e, 8);
|
||||
sha1_R3(block, e, a, b, c, d, 9);
|
||||
sha1_R3(block, d, e, a, b, c, 10);
|
||||
sha1_R3(block, c, d, e, a, b, 11);
|
||||
sha1_R3(block, b, c, d, e, a, 12);
|
||||
sha1_R3(block, a, b, c, d, e, 13);
|
||||
sha1_R3(block, e, a, b, c, d, 14);
|
||||
sha1_R3(block, d, e, a, b, c, 15);
|
||||
sha1_R3(block, c, d, e, a, b, 0);
|
||||
sha1_R3(block, b, c, d, e, a, 1);
|
||||
sha1_R3(block, a, b, c, d, e, 2);
|
||||
sha1_R3(block, e, a, b, c, d, 3);
|
||||
sha1_R3(block, d, e, a, b, c, 4);
|
||||
sha1_R3(block, c, d, e, a, b, 5);
|
||||
sha1_R3(block, b, c, d, e, a, 6);
|
||||
sha1_R3(block, a, b, c, d, e, 7);
|
||||
sha1_R3(block, e, a, b, c, d, 8);
|
||||
sha1_R3(block, d, e, a, b, c, 9);
|
||||
sha1_R3(block, c, d, e, a, b, 10);
|
||||
sha1_R3(block, b, c, d, e, a, 11);
|
||||
sha1_R4(block, a, b, c, d, e, 12);
|
||||
sha1_R4(block, e, a, b, c, d, 13);
|
||||
sha1_R4(block, d, e, a, b, c, 14);
|
||||
sha1_R4(block, c, d, e, a, b, 15);
|
||||
sha1_R4(block, b, c, d, e, a, 0);
|
||||
sha1_R4(block, a, b, c, d, e, 1);
|
||||
sha1_R4(block, e, a, b, c, d, 2);
|
||||
sha1_R4(block, d, e, a, b, c, 3);
|
||||
sha1_R4(block, c, d, e, a, b, 4);
|
||||
sha1_R4(block, b, c, d, e, a, 5);
|
||||
sha1_R4(block, a, b, c, d, e, 6);
|
||||
sha1_R4(block, e, a, b, c, d, 7);
|
||||
sha1_R4(block, d, e, a, b, c, 8);
|
||||
sha1_R4(block, c, d, e, a, b, 9);
|
||||
sha1_R4(block, b, c, d, e, a, 10);
|
||||
sha1_R4(block, a, b, c, d, e, 11);
|
||||
sha1_R4(block, e, a, b, c, d, 12);
|
||||
sha1_R4(block, d, e, a, b, c, 13);
|
||||
sha1_R4(block, c, d, e, a, b, 14);
|
||||
sha1_R4(block, b, c, d, e, a, 15);
|
||||
|
||||
/* Add the working vars back into digest[] */
|
||||
digest[0] += a;
|
||||
|
@ -207,63 +222,58 @@ static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &t
|
|||
transforms++;
|
||||
}
|
||||
|
||||
|
||||
static void buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS])
|
||||
static void sha1_buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS])
|
||||
{
|
||||
/* Convert the std::string (byte buffer) to a uint32_t array (MSB) */
|
||||
for (size_t i = 0; i < BLOCK_INTS; i++)
|
||||
{
|
||||
block[i] = (buffer[4*i+3] & 0xff)
|
||||
| (buffer[4*i+2] & 0xff)<<8
|
||||
| (buffer[4*i+1] & 0xff)<<16
|
||||
| (buffer[4*i+0] & 0xff)<<24;
|
||||
block[i] = static_cast<std::uint32_t>((buffer[4*i+3] & 0xff)
|
||||
| (buffer[4*i+2] & 0xff)<<8
|
||||
| (buffer[4*i+1] & 0xff)<<16
|
||||
| (buffer[4*i+0] & 0xff)<<24);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SHA1::SHA1()
|
||||
{
|
||||
reset(digest, buffer, transforms);
|
||||
sha1_reset(digest, buffer, transforms);
|
||||
}
|
||||
|
||||
|
||||
void SHA1::update(const std::string &s)
|
||||
{
|
||||
std::istringstream is(s);
|
||||
update(is);
|
||||
}
|
||||
|
||||
|
||||
void SHA1::update(std::istream &is)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
char sbuf[BLOCK_BYTES];
|
||||
is.read(sbuf, BLOCK_BYTES - buffer.size());
|
||||
buffer.append(sbuf, is.gcount());
|
||||
is.read(sbuf, static_cast<std::streamsize>(BLOCK_BYTES - buffer.size()));
|
||||
buffer.append(sbuf, static_cast<std::size_t>(is.gcount()));
|
||||
if (buffer.size() != BLOCK_BYTES)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint32_t block[BLOCK_INTS];
|
||||
buffer_to_block(buffer, block);
|
||||
transform(digest, block, transforms);
|
||||
sha1_buffer_to_block(buffer, block);
|
||||
sha1_transform(digest, block, transforms);
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Add padding and return the message digest.
|
||||
*/
|
||||
|
||||
std::string SHA1::final()
|
||||
std::string SHA1::final_()
|
||||
{
|
||||
/* Total number of hashed bits */
|
||||
uint64_t total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8;
|
||||
|
||||
/* Padding */
|
||||
buffer += 0x80;
|
||||
buffer += static_cast<char>(0x80);
|
||||
size_t orig_size = buffer.size();
|
||||
while (buffer.size() < BLOCK_BYTES)
|
||||
{
|
||||
|
@ -271,11 +281,11 @@ std::string SHA1::final()
|
|||
}
|
||||
|
||||
uint32_t block[BLOCK_INTS];
|
||||
buffer_to_block(buffer, block);
|
||||
sha1_buffer_to_block(buffer, block);
|
||||
|
||||
if (orig_size > BLOCK_BYTES - 8)
|
||||
{
|
||||
transform(digest, block, transforms);
|
||||
sha1_transform(digest, block, transforms);
|
||||
for (size_t i = 0; i < BLOCK_INTS - 2; i++)
|
||||
{
|
||||
block[i] = 0;
|
||||
|
@ -283,9 +293,9 @@ std::string SHA1::final()
|
|||
}
|
||||
|
||||
/* Append total_bits, split this uint64_t into two uint32_t */
|
||||
block[BLOCK_INTS - 1] = total_bits;
|
||||
block[BLOCK_INTS - 2] = (total_bits >> 32);
|
||||
transform(digest, block, transforms);
|
||||
block[BLOCK_INTS - 1] = static_cast<std::uint32_t>(total_bits);
|
||||
block[BLOCK_INTS - 2] = static_cast<std::uint32_t>(total_bits >> 32);
|
||||
sha1_transform(digest, block, transforms);
|
||||
|
||||
/* Hex std::string */
|
||||
std::ostringstream result;
|
||||
|
@ -296,32 +306,11 @@ std::string SHA1::final()
|
|||
}
|
||||
|
||||
/* Reset for next run */
|
||||
reset(digest, buffer, transforms);
|
||||
sha1_reset(digest, buffer, transforms);
|
||||
|
||||
return result.str();
|
||||
}
|
||||
|
||||
static std::vector<std::uint8_t> digest(const std::vector<std::uint8_t> &data)
|
||||
{
|
||||
auto s = SHA1();
|
||||
s.update(std::string(data.begin(), data.end()));
|
||||
auto hex = s.final();
|
||||
std::vector<std::uint8_t> bytes;
|
||||
|
||||
for (unsigned int i = 0; i < hex.length(); i += 2)
|
||||
{
|
||||
std::string byteString = hex.substr(i, 2);
|
||||
char byte = static_cast<std::uint8_t>(strtol(byteString.c_str(), NULL, 16));
|
||||
bytes.push_back(byte);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
} // namespace SHA1
|
||||
|
||||
namespace SHA512 {
|
||||
|
||||
struct sha512_state
|
||||
{
|
||||
std::uint64_t length;
|
||||
|
@ -502,7 +491,29 @@ static void sha_done(sha512_state& md, void *out)
|
|||
store64(md.state[i], static_cast<unsigned char*>(out)+(8*i));
|
||||
}
|
||||
|
||||
static std::vector<std::uint8_t> digest(const std::vector<std::uint8_t> &data)
|
||||
} // namespace SHA512
|
||||
|
||||
namespace xlnt {
|
||||
namespace detail {
|
||||
|
||||
std::vector<std::uint8_t> sha1(const std::vector<std::uint8_t> &data)
|
||||
{
|
||||
auto s = SHA1();
|
||||
s.update(std::string(data.begin(), data.end()));
|
||||
auto hex = s.final_();
|
||||
std::vector<std::uint8_t> bytes;
|
||||
|
||||
for (unsigned int i = 0; i < hex.length(); i += 2)
|
||||
{
|
||||
std::string byteString = hex.substr(i, 2);
|
||||
auto byte = static_cast<std::uint8_t>(strtol(byteString.c_str(), NULL, 16));
|
||||
bytes.push_back(byte);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> sha512(const std::vector<std::uint8_t> &data)
|
||||
{
|
||||
sha512_state md;
|
||||
sha_init(md);
|
||||
|
@ -512,16 +523,5 @@ static std::vector<std::uint8_t> digest(const std::vector<std::uint8_t> &data)
|
|||
return result;
|
||||
}
|
||||
|
||||
} // namespace SHA512
|
||||
|
||||
std::vector<std::uint8_t> SHA::sha1(const std::vector<std::uint8_t> &data)
|
||||
{
|
||||
return SHA1::digest(data);
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> SHA::sha512(const std::vector<std::uint8_t> &data)
|
||||
{
|
||||
return SHA512::digest(data);
|
||||
}
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
} // namespace detail
|
||||
} // namespace xlnt
|
||||
|
|
|
@ -1,10 +1,36 @@
|
|||
// Copyright (c) 2017 Thomas Fussell
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE
|
||||
//
|
||||
// @license: http://www.opensource.org/licenses/mit-license.php
|
||||
// @author: see AUTHORS file
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
class SHA
|
||||
{
|
||||
public:
|
||||
static std::vector<std::uint8_t> sha1(const std::vector<std::uint8_t> &data);
|
||||
static std::vector<std::uint8_t> sha512(const std::vector<std::uint8_t> &data);
|
||||
};
|
||||
namespace xlnt {
|
||||
namespace detail {
|
||||
|
||||
std::vector<std::uint8_t> sha1(const std::vector<std::uint8_t> &data);
|
||||
std::vector<std::uint8_t> sha512(const std::vector<std::uint8_t> &data);
|
||||
|
||||
}; // namespace detail
|
||||
}; // namespace xlnt
|
||||
|
||||
|
|
|
@ -280,11 +280,11 @@ std::vector<std::uint8_t> hash(hash_algorithm algorithm, const std::vector<std::
|
|||
{
|
||||
if (algorithm == hash_algorithm::sha512)
|
||||
{
|
||||
return SHA::sha512(input);
|
||||
return xlnt::detail::sha512(input);
|
||||
}
|
||||
else if (algorithm == hash_algorithm::sha1)
|
||||
{
|
||||
return SHA::sha1(input);
|
||||
return xlnt::detail::sha1(input);
|
||||
}
|
||||
|
||||
throw xlnt::exception("unsupported hash algorithm");
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
*
|
||||
**************************************************************************/
|
||||
|
||||
#include "miniz.hpp"
|
||||
#include <detail/miniz.hpp>
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wold-style-cast"
|
||||
|
|
Loading…
Reference in New Issue
Block a user