[C++] Strings hashing

08/24/2015 20:58 lamohaze#1
Heyo Com,

Suche jemanden, der mir teilweise erklären oder auch nur auf den richtigen Pfad leiten kann, wie ich bei C++ meine Strings hashe.


Danke im voraus,

sL1nky
08/24/2015 21:50 qqdev#2
Schritt 1: Hashing-Verfahren wählen.
08/24/2015 22:17 Padmak#3
Schritt 2: Nach einer Bibliothek für Hashes oder sonstige Implementierungen gewählter Hash-Funktion suchen

(Tipp: SHA3 ist ganz gut für kryptographische Anforderungen, MurmurHash oder XXHash für Sachen, bei denen die Kollisionen weniger wichtig sind als die Geschwindigkeit)

Padmak
08/24/2015 23:01 lamohaze#4
kenne mich da ehrlich 0 aus :c
08/25/2015 01:36 Padmak#5
Was hält dich davon ab, etwas zu recherchieren? Wir haben den Teil mit "auf den richtigen Pfad leiten" erfüllt, für den Rest bist du zuständig ;)

Bisschen Motivation:
~ Hier war ein Video von Shia LaBeouf's "Just Do It!" ~

Padmak
08/25/2015 11:34 ƬheGame#6
Library -> [Only registered and activated users can see links. Click Here To Register...]
Kann: SHA-1, SHA-2 (SHA-224, SHA-256, SHA-384, and SHA-512), SHA-3, Tiger, WHIRLPOOL, RIPEMD-128, RIPEMD-256, RIPEMD-160, RIPEMD-320

Habs nicht getestet aber etwa so müsste es funktionieren.
Code:
#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>

#include <string>

int main()
{
    CryptoPP::SHA3 sha3;
    std::string source = "Hello";
    std::string hash = "";
    CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha3, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));
    std::cout << "Unhashed: " << source << std::endl;
    std::cout << "Hashed: " << hash << std::endl;
}
08/25/2015 16:16 Jeoni#7
Kommt natürlich auf den Anwendungsfall an, aber die STL bietet mit [Only registered and activated users can see links. Click Here To Register...] bereits eine Möglichkeit Strings zu hashen.

Mit freundlichen Grüßen
Jeoni
09/01/2015 16:04 FiddleRust#8
Quote:
int hash = 0;
int offset = 'a' - 1;
for(string::const_iterator it=s.begin(); it!=s.end(); ++it) {
hash = hash << 1 | (*it - offset);
}
This could work :D
10/02/2015 21:51 xAxTer#9
Quote:
Originally Posted by ƬheGame View Post
Library -> [Only registered and activated users can see links. Click Here To Register...]
Kann: SHA-1, SHA-2 (SHA-224, SHA-256, SHA-384, and SHA-512), SHA-3, Tiger, WHIRLPOOL, RIPEMD-128, RIPEMD-256, RIPEMD-160, RIPEMD-320

Habs nicht getestet aber etwa so müsste es funktionieren.
Code:
#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>

#include <string>

int main()
{
    CryptoPP::SHA3 sha3;
    std::string source = "Hello";
    std::string hash = "";
    CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha3, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));
    std::cout << "Unhashed: " << source << std::endl;
    std::cout << "Hashed: " << hash << std::endl;
}
Yeah it's great and also libtom can be useful too
[Only registered and activated users can see links. Click Here To Register...]

Code:
//Hashes the input string using sha1
unsigned char* hSHA1(const std::string& input) {

    hash_state hs;
    sha1_init(&hs);
    unsigned char* checksum = new unsigned char[sha1_desc.hashsize];
    sha1_process(&hs, (const unsigned char*) input.c_str(), input.size());
    sha1_done(&hs, checksum);
    return checksum;
}
10/03/2015 02:54 Computerfreek#10
Quote:
Originally Posted by xAxTer View Post
Yeah it's great and also libtom can be useful too
[Only registered and activated users can see links. Click Here To Register...]

Code:
//Hashes the input string using sha1
unsigned char* hSHA1(const std::string& input) {

    hash_state hs;
    sha1_init(&hs);
    unsigned char* checksum = new unsigned char[sha1_desc.hashsize];
    sha1_process(&hs, (const unsigned char*) input.c_str(), input.size());
    sha1_done(&hs, checksum);
    return checksum;
}
Well, I did not test it, but why do you use dynamic raw arrays?
Whoever uses this will probably (and thought futher pretty surely) forget to delete the dynamically crafted array which will cause if the hashing is used more often, which is absolutely possible heavy memory leaks.

It should work perfectly with an usual std::vector, locally created and copied (in this example, moving should work of course, too).

Code:
std::vector hSHA1(const std::string& input) {
    hash_state hs;
    sha1_init(&hs);
	
    std::vector<unsigned char> checksum;
    checksum.resize(sha1_desc.hashsize);
	
    sha1_process(&hs, reinterpret_cast<const unsigned char*>(input.c_str()), input.size());
    sha1_done(&hs, &checksum[0]);
    return checksum;
}
I did not test this, but it should work. Correct me if I'm wrong.
But yet I think cryptopp is the better way to go. No specific reason then rather personal preference but why would you prefer to use C libraries instead of C++ ones when already programming in C++?