SHA1 Implementation
Stefan Mangard
sm1 at jhunix.hcf.jhu.edu
Thu Jun 1 04:48:44 UTC 2000
Hi,
I am currently working with bind 8.2.2-P5 and openSSL 0.9.5a. Both have an
implementation of the SHA1 hash function.
For some reason I get different hash values for the libraries, when using
a certain hash string. I actually wrote a little program to test it out,
which is at the end of the message.
I basically hash a binary string which I read from a file. For openSSL I
use the SHA1 function to hash - for the bind library it's called SHA (but
calculates actually SHA-1)
When I hash 50 or 60 bytes (or any lower length) of my string the hashes
are identical, but if I hash 70 bytes the hashes are different somehow.
Here is a lot of output and my programs. I put everything in because I am
not sure, what I do wrong to cause this strange effect.
Does anybody have an explaination for this?
Thanks,
Stefan
The output are byte values in decimal
The output, when using the bind library:
Hashing (length 50):
0:25:3:4:0:1:81:128:57:94:157:63:57:53:190:191:247:151:5:99:104:105:101:102:0:5:112:105:110:107:121:2:99:115:3:106:104:117:3:101:100:117:0:0:25:0:1:0:1:81:
Result:
58:177:89:103:69:213:73:77:237:52:208:122:96:151:14:169:250:142:111:109:
Hashing (length 60):
0:25:3:4:0:1:81:128:57:94:157:63:57:53:190:191:247:151:5:99:104:105:101:102:0:5:112:105:110:107:121:2:99:115:3:106:104:117:3:101:100:117:0:0:25:0:1:0:1:81:128:0:134:2:0:5:1:1:35:221:
Result:
19:191:170:197:79:198:51:74:127:117:145:141:33:226:135:20:118:12:45:198:
Hashing (length 70):
0:25:3:4:0:1:81:128:57:94:157:63:57:53:190:191:247:151:5:99:104:105:101:102:0:5:112:105:110:107:121:2:99:115:3:106:104:117:3:101:100:117:0:0:25:0:1:0:1:81:128:0:134:2:0:5:1:1:35:221:86:212:131:117:154:179:24:252:231:151:
Result:
167:122:213:154:179:115:47:129:124:221:105:243:52:110:125:228:33:141:102:56:
The output, when using the openSSL library:
Hashing (length 50):
0:25:3:4:0:1:81:128:57:94:157:63:57:53:190:191:247:151:5:99:104:105:101:102:0:5:112:105:110:107:121:2:99:115:3:106:104:117:3:101:100:117:0:0:25:0:1:0:1:81:
Result:
58:177:89:103:69:213:73:77:237:52:208:122:96:151:14:169:250:142:111:109:
Hashing (length 60):
0:25:3:4:0:1:81:128:57:94:157:63:57:53:190:191:247:151:5:99:104:105:101:102:0:5:112:105:110:107:121:2:99:115:3:106:104:117:3:101:100:117:0:0:25:0:1:0:1:81:128:0:134:2:0:5:1:1:35:221:
Result:
19:191:170:197:79:198:51:74:127:117:145:141:33:226:135:20:118:12:45:198:
Hashing (length 70):
0:25:3:4:0:1:81:128:57:94:157:63:57:53:190:191:247:151:5:99:104:105:101:102:0:5:112:105:110:107:121:2:99:115:3:106:104:117:3:101:100:117:0:0:25:0:1:0:1:81:128:0:134:2:0:5:1:1:35:221:86:212:131:117:154:179:24:252:231:151:
Result:
67:130:55:239:255:124:154:195:255:229:162:232:1:87:108:23:217:94:112:14:
Program the way I ran it for bind:
#include <stdio.h>
#include <malloc.h>
#include "sha.h"
#include "ctk_endian.h"
#include <toolkit.h>
//#include <openssl/sha.h>
extern u_int16_t DataOrder;
int main()
{
int i;
unsigned char *hash;
unsigned char data[1000];
FILE *fp;
int len;
DataOrder = 1;
len = 70;
hash = malloc(100);
fp = fopen("strange","r");
for (i=0; i<187 ; i++) {
data[i] = fgetc(fp);
}
fclose(fp);
printf("Hashing (length %i):\n",len);
for (i=0; i<len; i++) {
printf("%i:", data[i]);
}
printf("\nResult:\n");
SHA(data,len,hash);
for (i=0; i<20; i++) {
printf("%i:", hash[i]);
hash[i] = 0;
}
printf("\n");
free(hash);
return 0;
}
Program the way I ran it for openSSL:
#include <stdio.h>
#include <malloc.h>
//#include "sha.h"
//#include "ctk_endian.h"
//#include <toolkit.h>
#include <openssl/sha.h>
//extern u_int16_t DataOrder;
int main()
{
int i;
unsigned char *hash;
unsigned char data[1000];
FILE *fp;
int len;
// DataOrder = 1;
len = 70;
hash = malloc(100);
fp = fopen("strange","r");
for (i=0; i<187 ; i++) {
data[i] = fgetc(fp);
}
fclose(fp);
printf("Hashing (length %i):\n",len);
for (i=0; i<len; i++) {
printf("%i:", data[i]);
}
printf("\nResult:\n");
SHA1(data,len,hash);
for (i=0; i<20; i++) {
printf("%i:", hash[i]);
hash[i] = 0;
}
printf("\n");
free(hash);
return 0;
}
More information about the bind-users
mailing list