From be0cfca23695b7e412c4dbaa74ea726456e81d04 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Fri, 5 Mar 2021 06:51:58 +0000 Subject: Implement support for other SHA2 hash types. --- src/modules/m_sha2.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/modules/m_sha2.cpp (limited to 'src/modules/m_sha2.cpp') diff --git a/src/modules/m_sha2.cpp b/src/modules/m_sha2.cpp new file mode 100644 index 000000000..3c3560d5c --- /dev/null +++ b/src/modules/m_sha2.cpp @@ -0,0 +1,71 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2019 linuxdaemon + * Copyright (C) 2013, 2017-2018 Sadie Powell + * Copyright (C) 2012 Robby + * Copyright (C) 2009 Uli Schlachter + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2007-2008 Robin Burchell + * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2006, 2010 Craig Edwards + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/// $CompilerFlags: -Ivendor_directory("sha2") + + +#include "inspircd.h" +#include "modules/hash.h" + +#include + +template +class HashSHA2 : public HashProvider +{ + public: + HashSHA2(Module* parent, const std::string& Name, unsigned int osize, unsigned int bsize) + : HashProvider(parent, Name, osize, bsize) + { + } + + std::string GenerateRaw(const std::string& data) override + { + std::vector bytes(out_size); + SHA((unsigned char*)data.data(), data.size(), (unsigned char*)&bytes[0]); + return std::string(&bytes[0], bytes.size()); + } +}; + + +class ModuleSHA2 : public Module +{ + private: + HashSHA2 sha224algo; + HashSHA2 sha256algo; + HashSHA2 sha384algo; + HashSHA2 sha512algo; + + public: + ModuleSHA2() + : Module(VF_VENDOR, "Allows other modules to generate SHA-2 hashes.") + , sha224algo(this, "sha224", SHA224_DIGEST_SIZE, SHA224_BLOCK_SIZE) + , sha256algo(this, "sha256", SHA256_DIGEST_SIZE, SHA256_BLOCK_SIZE) + , sha384algo(this, "sha384", SHA384_DIGEST_SIZE, SHA384_BLOCK_SIZE) + , sha512algo(this, "sha512", SHA512_DIGEST_SIZE, SHA512_BLOCK_SIZE) + { + } +}; + +MODULE_INIT(ModuleSHA2) -- cgit v1.3.1-10-gc9f91