2020-04-25 03:23:37 +04:30
|
|
|
/*
|
2021-04-23 00:43:01 +04:30
|
|
|
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
2020-04-25 03:23:37 +04:30
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-25 03:23:37 +04:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibCrypto/Hash/HashFunction.h>
|
|
|
|
|
|
2022-02-15 21:36:46 +02:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:49:34 +03:30
|
|
|
# include <AK/ByteString.h>
|
2022-02-15 21:36:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
2023-01-04 14:11:12 -05:00
|
|
|
namespace Crypto::Hash {
|
2020-04-25 03:23:37 +04:30
|
|
|
|
|
|
|
|
namespace SHA1Constants {
|
|
|
|
|
|
|
|
|
|
constexpr static u32 InitializationHashes[5] { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
|
|
|
|
|
|
|
|
|
|
constexpr static u32 RoundConstants[4] {
|
|
|
|
|
0X5a827999,
|
|
|
|
|
0X6ed9eba1,
|
|
|
|
|
0X8f1bbcdc,
|
|
|
|
|
0Xca62c1d6,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 22:28:19 +01:00
|
|
|
class SHA1 final : public HashFunction<512, 160> {
|
2020-04-25 03:23:37 +04:30
|
|
|
public:
|
2020-12-19 15:56:15 +01:00
|
|
|
using HashFunction::update;
|
|
|
|
|
|
2020-04-25 03:23:37 +04:30
|
|
|
SHA1()
|
|
|
|
|
{
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void update(u8 const*, size_t) override;
|
2020-04-25 03:23:37 +04:30
|
|
|
|
|
|
|
|
virtual DigestType digest() override;
|
|
|
|
|
virtual DigestType peek() override;
|
|
|
|
|
|
2023-01-04 12:19:27 -05:00
|
|
|
static DigestType hash(u8 const* data, size_t length)
|
2020-04-25 03:23:37 +04:30
|
|
|
{
|
|
|
|
|
SHA1 sha;
|
|
|
|
|
sha.update(data, length);
|
|
|
|
|
return sha.digest();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:19:27 -05:00
|
|
|
static DigestType hash(ByteBuffer const& buffer) { return hash(buffer.data(), buffer.size()); }
|
|
|
|
|
static DigestType hash(StringView buffer) { return hash((u8 const*)buffer.characters_without_null_termination(), buffer.length()); }
|
2020-04-25 03:23:37 +04:30
|
|
|
|
2022-02-15 21:36:46 +02:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:49:34 +03:30
|
|
|
virtual ByteString class_name() const override
|
2020-04-25 03:23:37 +04:30
|
|
|
{
|
|
|
|
|
return "SHA1";
|
2022-01-03 22:27:02 +01:00
|
|
|
}
|
2022-02-15 21:36:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
2023-01-04 12:19:27 -05:00
|
|
|
virtual void reset() override
|
2020-04-25 03:23:37 +04:30
|
|
|
{
|
2020-04-25 05:07:24 +04:30
|
|
|
m_data_length = 0;
|
|
|
|
|
m_bit_length = 0;
|
2020-04-25 03:23:37 +04:30
|
|
|
for (auto i = 0; i < 5; ++i)
|
|
|
|
|
m_state[i] = SHA1Constants::InitializationHashes[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2022-04-01 20:58:27 +03:00
|
|
|
inline void transform(u8 const*);
|
2020-04-25 03:23:37 +04:30
|
|
|
|
2021-05-18 20:41:00 +02:00
|
|
|
u8 m_data_buffer[BlockSize] {};
|
2020-04-25 03:23:37 +04:30
|
|
|
size_t m_data_length { 0 };
|
|
|
|
|
|
|
|
|
|
u64 m_bit_length { 0 };
|
|
|
|
|
u32 m_state[5];
|
|
|
|
|
|
|
|
|
|
constexpr static auto FinalBlockDataSize = BlockSize - 8;
|
|
|
|
|
constexpr static auto Rounds = 80;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|