Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <ctime>
- #include <string>
- using namespace std;
- vector <string > ciagi;
- vector <unsigned int> djb_tab;
- vector <unsigned int> adler_tab;
- unsigned int djb(string text) {
- int H = 5381;
- for (int i = 0; i < text.size(); i++) {
- H = H * 32 + H + (int)text[i];
- }
- return H;
- }
- unsigned int adler32(string text) {
- int A = 1;
- int B = 0;
- int p = 65521; // NAJWIEKSZA LICZBA PIERWSZA W ZAKRESIE DO 16 BITOW
- for (int i = 0; i < text.size(); i++) {
- A = (A + (int)text[i]) % p;
- B = (B + A) % p;
- }
- return B * 65536 + A; // B * 65536 -> PRZESUNIECIE O 16 BITOW
- }
- void collision(int D, int N) {
- srand(time(NULL));
- string ciag = "";
- for (int i = 0; i < N; i++) {
- ciag = "";
- for (int j = 0; j < D; j++) {
- ciag += static_cast<char>((rand() % 122) + 48);
- }
- ciagi.push_back(ciag);
- }
- long long K = 0;
- cout << endl;
- int metoda;
- cout << "Wybierz metoda: " << endl;
- cout << "1. DJB" << endl;
- cout << "2. ADLER32" << endl;
- cin >> metoda;
- switch (metoda)
- {
- case 1:
- for (int i = 0; i < ciagi.size(); i++) {
- djb_tab.push_back(djb(ciagi[i]));
- //cout << ciagi[i] << endl;
- }
- for (int i = 0; i < djb_tab.size(); i++) {
- for (int j = i; j < djb_tab.size(); j++) {
- if (djb_tab[i] == djb_tab[j] && i != j) {
- K++;
- if (K < 3) {
- cout << "Kolizja " << K << endl;
- cout << "Ciag " << i << ": " << ciagi[i] << endl;
- cout << "Ciag " << j << ": " << ciagi[j] << endl;
- cout << endl << "Suma kontrolna: " << djb_tab[i] << endl;
- cout << "****************************************************************************" << endl;
- }
- }
- }
- }
- cout << "DJB\t" << "D = " << D << " N = " << N << " K = " << K << endl;
- break;
- case 2:
- for (int i = 0; i < ciagi.size(); i++) {
- adler_tab.push_back(adler32(ciagi[i]));
- //cout << ciagi[i] << endl;
- }
- for (int i = 0; i < adler_tab.size(); i++) {
- for (int j = i; j < adler_tab.size(); j++) {
- if (adler_tab[i] == adler_tab[j] && i != j) {
- K++;
- if (K < 3) {
- cout << "Kolizja " << K << endl;
- cout << "Ciag " << i << ": " << ciagi[i] << endl;
- cout << "Ciag " << j << ": " << ciagi[j] << endl;
- cout << endl << "Suma kontrolna: " << adler_tab[i] << endl;
- cout << "****************************************************************************" << endl;
- }
- }
- }
- }
- cout << "ADLER32\t" << "D = " << D << " N = " << N << " K = " << K << endl;
- break;
- default:
- break;
- }
- }
- int main() {
- string test = "test"; // ADLER = 73204161 & DJB = 2090756197
- string tset = "tset"; // ADLER = 74121665 & DJB = 209077981
- //cout << "DJB: " << djb(test) << " " << djb(tset) << endl;
- //cout << "ADLER: " << adler32(test) << " " << adler32(tset) << endl;
- collision(100, 100000);
- system("PAUSE");
- }
- ////// OPEN PGP STUDIO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement