比较2个文件中的子字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了比较2个文件中的子字符串相关的知识,希望对你有一定的参考价值。
我有2个文件:shadow.txt和mytab2411.txt。 “shadow.txt”包含密码哈希值,而“mytab2411.txt”是包含密码及其相应哈希值的密码查找文件。我想要做的是使用“mytab2411.txt”通过比较“shadow.txt”和“mytab2411.txt”中的散列来查找“shadow.txt”的密码。
我有两个问题:
- 我不知道如何从mytab2411.txt获取密码文本。
- 我得到此输出而不是所需的输出:
用户ID:pyc 1 -password(未找到)
用户ID:pyc 2 -password(未找到)
用户ID:pyc 3 -password(未找到)
用户ID:pyc 4 -password(未找到)
用户ID:pyc 5 -password(未找到)
用户ID:pyc 6 -password(未找到)
shadow.txt
pyc1:$1$$Tnq7a6/C1wwyKyt0V/.BP/:17482:0:99999:7:::
pyc2:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1:17482:0:99999:7:::
pyc3:$1$$zZQKNjRd94GHyYOwXuStf0:17482:0:99999:7:::
pyc4:$6$iKYSRG68$STdY8TCgoCaNfSUcyCwSBlVekdjs0P3qXtwxSbgpQpMUnHJRRSHOT5amoR24IqZBTPNWuIfO.uhZEnGLuE4q/.:17482:0:99999:7:::
pyc5:$6$$FDqvMBbQOCyKP9uBL8E6TAEupCh72v.3/ow4fZ5HpZ/0NS7LBifFS9nJdzc/u2OEhUnRF9yC4Lw23hHjD1EmD.:17482:0:99999:7:::
pyc6:$6$$LfCuhKecDtIfX77LOTWD1PjhF1IC0hBzjxckEthmoT8mVbxKH3qJzFgEi/P9GN1mptR4WPiwuh69X/41M6pHW1:17482:0:99999:7:::
mytab2411.txt(缩短版本,因为有超过200k行)
apple:$1$$Tnq7a6/C1wwyKyt0V/.BP/
apple:$6$$vTqYXuMRNbK5N1xiTvUKcJuKVmEQyPtgUiyawaEBMwknJ3AQoOvPpr2RrANRxDTS.qo7rQuFvxZcUkT31W6uG/
banana:$1$$zZQKNjRd94GHyYOwXuStf0
banana:$6$$5iQBiKBv7vIGqC5iQJOVUpzgnSO0P.pMQ.Guwczcn9nQSu61IVKT9GU4IEYjb5WbsBLaIfZ3io59M4oac.W0/1
orange:$1$$Ro.kDk5GNLNQbdJyDEovy1
orange:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1
期望的输出
user id : pyc1 – password found => apple
user id : pyc2 – password found => orange
user id : pyc3 – password found => banana
Data Error: Invalid entry found in the shadow file. (skipped)
user id : pyc5 – password (NOT FOUND)
user id : pyc6 – password (NOT FOUND)
主要代码
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
int printDataError(){
printf("Data error: Invalid entry found in the shadow file. (Skipped)
");
}
int main(void)
{
{
char buf[1024];
char **arr1 = NULL;
char **arr2 = NULL;
int size1 = 0;
int size2 = 0;
FILE * f1, *f2;
f1 = fopen("shadow.txt", "r");
f2 = fopen("mytab2411.txt", "r");
// Allocate memory for shadow.txt
while(fgets(buf, 1024, f1))
{
size1++;
arr1 = realloc(arr1, sizeof(char*) * size1);
arr1[size1 - 1] = strdup(buf);
}
// Allocate memory for mytab2411.txt
while(fgets(buf, 1024, f2))
{
size2++;
arr2 = realloc(arr2, sizeof(char*) * size2);
arr2[size2 - 1] = strdup(buf);
}
char line[1000]; //Allocate max number of characters in a line for shadow.txt
char line2[1000]; //Allocate max number of characters in a line for mytab2411.txt
char hash[1000]; //Allocate max number of characters in a hash (substring of a line) for shadow.txt
char hash2[1000]; //Allocate max number of characters in a hash (substring of a line) for shadow.txt
char md5[5]= "$1$$"; // Define string to be searched for md5
char sha512[5]="$6$$"; //Define string to be searched for sha512
char *ret; // Used for shadow.txt
char *ret2; //Used for shadow.txt
char * ret3; //Used for mytab2411.txt
char * ret4; //Used for mytab2411.txt
// Read shadow.txt line by line
for(int i = 0; i < size1; i++) {
memset(hash, ' ', sizeof(hash));
strcpy(line, arr1[i]);
//Search for md5 in shadow.txt
md5[4]=' ';
ret = strstr(line, md5);
//Search for sha512 in shadow.txt
sha512[4]=' ';
ret2 = strstr(line, sha512);
// Copies md5 hash to the variable hash if md5 is detected in shadow.txt
if (ret){
strncpy(hash, line+5, 26);
hash[26] = ' ';
//printf("pyc %d hash: %s
", i+1,hash);
}
// Copies sha512 hash to the variable hash if sha512 is detected in shadow.txt
else if (ret2){
strncpy(hash, line+5, 90);
hash[90] = ' ';
//printf("pyc %d hash: %s
", i+1,hash);
}
// Read mytab2411.txt line by line
for(int j = 0; j < size2; j++){
memset(hash2, ' ', sizeof(hash2));
strcpy(line2, arr2[j]);
//Search for md5 in mytab2411.txt
md5[4]=' ';
ret3 = strstr(line2, md5);
//Search for sha512 in mytab2411.txt
sha512[4]=' ';
ret4 = strstr(line2, sha512);
// Copies sha512 hash to the variable hash if md5 is detected in mytab2411.txt
if (ret3){
strcpy(hash2, &line2[strlen(line2) - 27]);
//printf("Line %d hash: %s
", j+1,hash2);
}
// Copies sha512 hash to the variable hash if sha512 is detected in mytab2411.txt
else if (ret4){
strcpy(hash2, &line2[strlen(line2) - 91]);
//printf("Line %d hash: %s
", j+1,hash2);
}
}//End of "for(int j = 0; j < size2; j++)" loop
// Compares the hash in shadow.txt (hash) and hash in mytab2411.txt (hash2).
if(strcmp(hash,hash2) == 0)
printf("user id: pyc %d - password found =>
",i+1);
else if (strcmp(hash,hash2) != 0)
printf("user id: pyc %d -password <NOT FOUND>
",i+1);
else
printDataError();
}//End of "for (int i = 0; i < size1; i++)" loop
return 0;
} //End of main
答案
两个文件中的密码已经过哈希处理。你不应该自己计算任何哈希值。相反,您应该将密码作为文本阅读,并简单地比较它们。
以上是关于比较2个文件中的子字符串的主要内容,如果未能解决你的问题,请参考以下文章
片段中ListView的setOnItemClickListener
如何在 C++ 程序中的 2 个特定字符之间比较 2 个文件中的文本行