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