aboutsummaryrefslogtreecommitdiff
path: root/src/sha1.h
blob: c04947373e012ca417cbe8ffdf8f9ea566c93484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* sha1.h

Copyright (c) 2005 Michael D. Leonhard

http://tamale.net/

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, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

#ifndef SHA1_HEADER
typedef unsigned int Uint32;

class SHA1
{
	private:
		// fields
		Uint32 H0, H1, H2, H3, H4;
		unsigned char bytes[64];
		int unprocessedBytes;
		Uint32 size;
		void process();
	public:
		SHA1();
		~SHA1();
		void addBytes( const char* data, int num );
		unsigned char* getDigest();
		// utility methods
		static Uint32 lrot( Uint32 x, int bits );
		static void storeBigEndianUint32( unsigned char* byte, Uint32 num );
		static void hexPrinter( unsigned char* c, int l );
};

#define SHA1_HEADER
#endif
"hl pps">"jthread/jmutexautolock.h" #include <sstream> #include <set> #include "strfnd.h" #include "util/string.h" #include "log.h" #include "filesys.h" BanManager::BanManager(const std::string &banfilepath): m_banfilepath(banfilepath), m_modified(false) { try{ load(); } catch(SerializationError &e) { infostream<<"WARNING: BanManager: creating " <<m_banfilepath<<std::endl; } } BanManager::~BanManager() { save(); } void BanManager::load() { JMutexAutoLock lock(m_mutex); infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl; std::ifstream is(m_banfilepath.c_str(), std::ios::binary); if(is.good() == false) { infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl; throw SerializationError("BanManager::load(): Couldn't open file"); } while(!is.eof() && is.good()) { std::string line; std::getline(is, line, '\n'); Strfnd f(line); std::string ip = trim(f.next("|")); std::string name = trim(f.next("|")); if(!ip.empty()) { m_ips[ip] = name; } } m_modified = false; } void BanManager::save() { JMutexAutoLock lock(m_mutex); infostream << "BanManager: saving to " << m_banfilepath << std::endl; std::ostringstream ss(std::ios_base::binary); for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) ss << it->first << "|" << it->second << "\n"; if (!fs::safeWriteToFile(m_banfilepath, ss.str())) { infostream << "BanManager: failed saving to " << m_banfilepath << std::endl; throw SerializationError("BanManager::save(): Couldn't write file"); } m_modified = false; } bool BanManager::isIpBanned(const std::string &ip) { JMutexAutoLock lock(m_mutex); return m_ips.find(ip) != m_ips.end(); } std::string BanManager::getBanDescription(const std::string &ip_or_name) { JMutexAutoLock lock(m_mutex); std::string s = ""; for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) { if (it->first == ip_or_name || it->second == ip_or_name || ip_or_name == "") { s += it->first + "|" + it->second + ", "; } } s = s.substr(0, s.size() - 2); return s; } std::string BanManager::getBanName(const std::string &ip) { JMutexAutoLock lock(m_mutex); StringMap::iterator it = m_ips.find(ip); if (it == m_ips.end()) return ""; return it->second; } void BanManager::add(const std::string &ip, const std::string &name) { JMutexAutoLock lock(m_mutex); m_ips[ip] = name; m_modified = true; } void BanManager::remove(const std::string &ip_or_name) { JMutexAutoLock lock(m_mutex);