
This commit un-deprecates DeprecatedString, and repurposes it as a byte string. As the null state has already been removed, there are no other particularly hairy blockers in repurposing this type as a byte string (what it _really_ is). This commit is auto-generated: $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \ Meta Ports Ladybird Tests Kernel) $ perl -pie 's/\bDeprecatedString\b/ByteString/g; s/deprecated_string/byte_string/g' $xs $ clang-format --style=file -i \ $(git diff --name-only | grep \.cpp\|\.h) $ gn format $(git ls-files '*.gn' '*.gni')
82 lines
1.7 KiB
C++
82 lines
1.7 KiB
C++
/*
|
|
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibCrypto/Hash/HashFunction.h>
|
|
|
|
#ifndef KERNEL
|
|
# include <AK/ByteString.h>
|
|
#endif
|
|
|
|
namespace Crypto::Hash {
|
|
|
|
namespace SHA1Constants {
|
|
|
|
constexpr static u32 InitializationHashes[5] { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
|
|
|
|
constexpr static u32 RoundConstants[4] {
|
|
0X5a827999,
|
|
0X6ed9eba1,
|
|
0X8f1bbcdc,
|
|
0Xca62c1d6,
|
|
};
|
|
|
|
}
|
|
|
|
class SHA1 final : public HashFunction<512, 160> {
|
|
public:
|
|
using HashFunction::update;
|
|
|
|
SHA1()
|
|
{
|
|
reset();
|
|
}
|
|
|
|
virtual void update(u8 const*, size_t) override;
|
|
|
|
virtual DigestType digest() override;
|
|
virtual DigestType peek() override;
|
|
|
|
static DigestType hash(u8 const* data, size_t length)
|
|
{
|
|
SHA1 sha;
|
|
sha.update(data, length);
|
|
return sha.digest();
|
|
}
|
|
|
|
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()); }
|
|
|
|
#ifndef KERNEL
|
|
virtual ByteString class_name() const override
|
|
{
|
|
return "SHA1";
|
|
}
|
|
#endif
|
|
|
|
virtual void reset() override
|
|
{
|
|
m_data_length = 0;
|
|
m_bit_length = 0;
|
|
for (auto i = 0; i < 5; ++i)
|
|
m_state[i] = SHA1Constants::InitializationHashes[i];
|
|
}
|
|
|
|
private:
|
|
inline void transform(u8 const*);
|
|
|
|
u8 m_data_buffer[BlockSize] {};
|
|
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;
|
|
};
|
|
|
|
}
|