/* * Add some data to the sha1 object. */ void sha1AddData(sha1Obj sha1, const unsigned char *data, unsigned dataLen) { sha1Inst *sinst = (sha1Inst *) sha1; unsigned toMove; unsigned blocks; if(sinst->isDone) { /* * Log some kind of error here... */ return; } /* * First deal with partial buffered block */ if(sinst->bufBytes != 0) { toMove = SHS_BLOCKSIZE - sinst->bufBytes; if(toMove > dataLen) { toMove = dataLen; } memmove(sinst->dataBuf+sinst->bufBytes, data, toMove); data += toMove; dataLen -= toMove; sinst->bufBytes += toMove; if(sinst->bufBytes == SHS_BLOCKSIZE) { shsUpdate(&sinst->context, sinst->dataBuf, SHS_BLOCKSIZE); sinst->bufBytes = 0; } } /* * Now the bulk of the data, in a multiple of full blocks */ blocks = dataLen / SHS_BLOCKSIZE; toMove = blocks * SHS_BLOCKSIZE; if(toMove != 0) { shsUpdate(&sinst->context, data, toMove); data += toMove; dataLen -= toMove; } /* * Store any remainder in dataBuf */ if(dataLen != 0) { memmove(sinst->dataBuf, data, dataLen); sinst->bufBytes = dataLen; } }
BSTR CMUSHclientDoc::Hash(LPCTSTR Text) { CString strResult; SHS_INFO shsInfo; shsInit (&shsInfo); shsUpdate (&shsInfo, (unsigned char *) (const char *) Text, strlen (Text)); shsFinal (&shsInfo); for (int i = 0; i < NUMITEMS (shsInfo.digest); i++) strResult += CFormat ("%08x", shsInfo.digest [i]); return strResult.AllocSysString(); } // end of CMUSHclientDoc::Hash
static krb5_error_code k5_sha1_hash(unsigned int icount, const krb5_data *input, krb5_data *output) { SHS_INFO ctx; unsigned int i; if (output->length != SHS_DIGESTSIZE) return(KRB5_CRYPTO_INTERNAL); shsInit(&ctx); for (i=0; i<icount; i++) shsUpdate(&ctx, (unsigned char *) input[i].data, input[i].length); shsFinal(&ctx); for (i=0; i<(sizeof(ctx.digest)/sizeof(ctx.digest[0])); i++) { store_32_be(ctx.digest[i], &output->data[i*4]); } return(0); }
/* * Obtain a pointer to completed message digest, and the length of the digest. */ unsigned char *sha1Digest(sha1Obj sha1) { sha1Inst *sinst = (sha1Inst *) sha1; if(!sinst->isDone) { /* * Deal with partial resid block */ if(sinst->bufBytes != 0) { shsUpdate(&sinst->context, sinst->dataBuf, sinst->bufBytes); sinst->bufBytes = 0; } shsFinal(&sinst->context); sinst->isDone = 1; } /* * FIXME - should do explicit conversion to char array....? */ return (unsigned char *)sinst->context.digest; }
static int myhash (lua_State *L) { // get text to hash size_t textLength; const char * text = luaL_checklstring (L, 1, &textLength); char buf [50]; SHS_INFO shsInfo; shsInit (&shsInfo); shsUpdate (&shsInfo, (UC *) text, textLength); shsFinal (&shsInfo); sprintf (buf, "%08x%08x%08x%08x%08x", shsInfo.digest [0], shsInfo.digest [1], shsInfo.digest [2], shsInfo.digest [3], shsInfo.digest [4] ); lua_pushstring (L, buf); return 1; // number of result fields } // end of myhash
char * shs_crypt(const unsigned char * pw, const Int pl, const unsigned char * sp, const Int sl, char * passwd) { uChar *p; uChar final[SHS_DIGEST_SIZE]; Int i,j; SHS_CTX ctx,ctx1; uInt l; shsInit(&ctx); /* The password first, since that is what is most unknown */ shsUpdate(&ctx,pw,pl); /* Then our magic string */ shsUpdate(&ctx,(uChar *)"$2$",3); /* Then the raw salt */ shsUpdate(&ctx,sp,sl); /* Then just as many characters of the shs(pw,salt,pw) */ shsInit(&ctx1); shsUpdate(&ctx1,pw,pl); shsUpdate(&ctx1,sp,sl); shsUpdate(&ctx1,pw,pl); shsFinal(&ctx1,final); for(i = pl; i > 0; i -= SHS_DIGEST_SIZE) shsUpdate(&ctx,final,i>SHS_DIGEST_SIZE ? SHS_DIGEST_SIZE : i); /* Don't leave anything around in vm they could use. */ memset(final,0,sizeof final); /* Then something really weird... */ for (j=0,i = pl; i ; i >>= 1) if(i&1) shsUpdate(&ctx, final+j, 1); else