本文章源于本人github学生管理系统项目的某个想法:https://github.com/oyhxgo/Student-Performance-Management-System
该项目任在完善期
哈希函数,为了了解原理,我先写了一个大致设想
//最初设想
#include<bits/stdc++.h>
using namespace std;
const int base=131;//这里是将字符串看成的进制
int hash1(string str) {
int ans=0;
for(int i=0;i<str.size();++i)
ans=ans*base+(int)str[i];
return ans;
}
int main(){
string a="abcdefg",b;
int ans=hash1(a);
cout<<ans<<endl;
cin>>b;
if(hash1(b)==hash1(a))
cout<<"good!";
else
cout<<"bad";
return 0;
}
斯,然后我研究了正统的哈希函数
//初学哈希,返回值为size_t(%llu),竟然属于整型
#include<bits/stdc++.h>
using namespace std;
int main(){
string a="123456",b;
hash<string>hash1;
cout<<hash1(a)<<endl;
cin>>b;
cout<<hash1(b)<<endl;
if(hash1(a)==hash1(b))
cout<<"good!";
else
cout<<"bad";
return 0;
}
加密性还是差,加点盐吧
//尝试实现哈希加盐
//首先测试随机生成字符串函数
#include<bits/stdc++.h>
using namespace std;
string rand_str(const int len)
{
srand(time(NULL)); //以时间为种子;
string str;
char c;
int i;
for(i = 0;i < len;i ++)
{
c = 'a' + rand()%26;
str.push_back(c); /*push_back()是string类尾插函数。这里插入随机字符c*/
}
return str;
}
int main()
{
string str;
str = rand_str(20);
cout << str << endl;
string a="123456",b;
a.append(str);
hash<string>hash1;
cout<<hash1(a)<<endl;
cin>>b;b.append(str);
cout<<hash1(b)<<endl;
if(hash1(a)==hash1(b))
cout<<"good!";
else
cout<<"bad";
return 0;
}
呦西,非常的好,尝试写入密码管理系统
#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
#define MAX 1000//用户密码最长为1000
time_t t = time(nullptr); // 获取1970年到现在的毫秒数
struct tm* now = localtime(&t); // 把毫秒转换为日期时间的结构体
//用户的属性
typedef struct log {
char username[MAX][MAX];//用户名
size_t password[MAX];
int num;
char salt[MAX][MAX];
} user;
FILE *logfile;
//开始录入用户名和密码
void Inituser(user *&U) {
U = new user;//分配空间
}
string rand_str(const int len) //随机字符串生成(盐)
{
srand(time(NULL)); //以时间为种子;
string str;
char c;
int i;
for(i = 0;i < len;i ++)
{
c = 'a' + rand()%26;
str.push_b