Probably not the normal use for this channel, but *shrug*, can't hurt. I was attempting to code a sha256 hash function, for hashing a password before sending it over the open net. (Sounds like it's better then md5 for this)
getting started, I found some psedocode at http://en.wikipedia.org/wiki/SHA2 and went to work. decided a blank string would be the best, It gives me 74525b2e06b6cfebaa347250d2a6c6c9a5438fbbd4b44ffefe68dcdd7b1d1206 but according to wikipedia above, it should be e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -- Nathan Coulson (conathan) ------ Location: British Columbia, Canada Timezone: PST (-8) Webpage: http://www.nathancoulson.com
#include <stdio.h> #include <string.h> #include <stdint.h> //created from http://en.wikipedia.org/wiki/SHA2 unsigned int k[64]={ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; unsigned int rRt(unsigned int i, unsigned char j) { return (i>>j) | (i << (32-j)); } void endianFlip(unsigned int *w) { unsigned char c[4]; int i; for(i=0; i<64; i++) { *(int*)c=w[i]; w[i]= c[0]<<24 | c[1]<<16 | c[2]<<8 | c[3]; } } void sha256S(unsigned int hsh[8], char *str, int length) { int i; unsigned int a,b,c,d,e,f,g,h; unsigned int s0, s1, maj, t1,t2,ch; unsigned int w[64]; uint64_t tl=length; hsh[0]=0x6a09e667; hsh[1]=0xbb67ae85; hsh[2]=0x3c6ef372; hsh[3]=0xa54ff53a; hsh[4]=0x510e527f; hsh[5]=0x9b05688c; hsh[6]=0x1f83d9ab; hsh[7]=0x5be0cd19; while(length>=-1) { if(length>=64) { memcpy(w, str, 64); length-=64; } else if (length>56) { *(length+(char*)w)=0x80; memset(length+1+(char*)w, 0, 64-length-1); memcpy(w, str, length); length=-1; } else { if(length>=0) *(length+(char*)w)=0x1; //*(length+(char*)w)=0x80; memset(length+1+(char*)w, 0, 56-length-1); memcpy(w, str, length); memcpy(56+(char*)w, &tl, 8); length=-2; for(i=0; i<64; i++) fprintf(stderr, "%2x", ((unsigned char*)w)[i]); fprintf(stderr, "\n"); } //extend to 64 32bit words for(i=16; i<64; i++) { s0= rRt(w[i-15], 7) ^ rRt(w[i-15],18) ^ (w[i-15]>>3); s1= rRt(w[i-2], 17) ^ rRt(w[i-2],19) ^ (w[i-2]>>10); w[i]=w[i-16]+s0+w[i-7]+s1; } a=hsh[0]; b=hsh[1]; c=hsh[2]; d=hsh[3]; e=hsh[4]; f=hsh[5]; g=hsh[6]; h=hsh[7]; //main loop for(i=0; i<64; i++) { s0=rRt(a,2) ^ rRt(a,13) ^ rRt(a,22); maj=(a&b) ^ (a&c) ^ (b&c); t2=s0+maj; s1=rRt(e,6) ^ rRt(e,11) ^ rRt(e, 25); ch=(e&f) ^ ((~e)&g); t1=h+s1+ch+k[i]+w[i]; h=g; g=f; f=e; e=d+t1; d=c; c=b; b=a; a=t1+t2; hsh[0]=a; hsh[1]=b; hsh[2]=c; hsh[3]=d; hsh[4]=e; hsh[5]=f; hsh[6]=g; hsh[7]=h; } } } int main() { int i; //char *str="The quick brown fox jumps over the lazy dog"; char *str=""; unsigned int z=0x98765432; unsigned int h[8]; //fprintf(stderr, "%x %x %x %x\n", ((uint8_t*)&z)[0], ((uint8_t*)&z)[1], ((uint8_t*)&z)[2], ((uint8_t*)&z)[3]); //endianFlip(&z); //fprintf(stderr, "%x %x %x %x\n", ((uint8_t*)&z)[0], ((uint8_t*)&z)[1], ((uint8_t*)&z)[2], ((uint8_t*)&z)[3]); sha256S(h, str, strlen(str)); fprintf(stderr, "%s\n", str); for(i=0; i<8; i++) { fprintf(stderr, "%8x", h[i]); } fprintf(stderr, "\n"); }
-- http://linuxfromscratch.org/mailman/listinfo/lfs-chat FAQ: http://www.linuxfromscratch.org/faq/ Unsubscribe: See the above information page
