void sha256_salt_to_str(const u8int *data, size_t data_sz, u8int *salt, size_t salt_sz, u8int *outputBuffer, u8int *hash_digest) { SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, data, data_sz); SHA256_Update(&sha256, salt, salt_sz); SHA256_Final(hash_digest, &sha256); //convert the digest to a string digest_to_string(hash_digest, outputBuffer); //sucess! return; }
static int sha1_digest(lua_State* L) { SHA1_CTX ctx; uint8_t digest[SHA1_DIGEST_SIZE]; char digestString[SHA1_DIGEST_STRING_SIZE + 1] = {0}; size_t len = 0; const char* input = luaL_checklstring(L, 1, &len); SHA1_Init(&ctx); SHA1_Update(&ctx, (const uint8_t*) input, len); SHA1_Final(&ctx, digest); digest_to_string(digest, digestString); lua_pushlstring(L, digestString, SHA1_DIGEST_STRING_SIZE); return 1; }
void sha256_to_str(const u8int *data, size_t data_sz, u8int *outputBuffer, u8int *hash_digest) { SHA256_CTX sha256; static u8int __digest__[SHA256_DIGEST_LENGTH]; if(hash_digest == NULL) hash_digest = __digest__; SHA256_Init(&sha256); SHA256_Update(&sha256, data, data_sz); SHA256_Final(hash_digest, &sha256); //convert the digest to a string digest_to_string(hash_digest, outputBuffer); //sucess! return; }
int scanhash_dcrypt(int thr_id, uint32_t *pdata, unsigned char *digest, const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done, int num_iter) { uint32_t block[20], hash[8]; uint32_t nNonce = pdata[19] - 1; const uint32_t Htarg = ptarget[7]; //the last element in the target is the first 32 bits of the target int i; bool completed; int missed = 0; //copy the block (first 80 bytes of pdata) into block memcpy(block, pdata, 80); SHA256_CTX halfstate,fullstate; SHA256_Init(&halfstate); SHA256_Update(&halfstate,&block,sizeof(uint32_t)*19); do { //increment nNonce block[19] = ++nNonce; //completed = dcrypt((u8int*)block, 80, digest, hash, num_iter); memcpy(&fullstate,&halfstate,sizeof(SHA256_CTX)); SHA256_Update(&fullstate,&block[19],sizeof(uint32_t)); SHA256_Final((u8int *)hash, &fullstate); completed = dcrypt_fast((u8int*)block, 80,hash);/**/ if (!completed) { missed += 1; continue; } /* // check optimized hash against previous hash function uint32_t hash2[8]; int c2 = dcrypt((u8int*)block, 80, digest, hash2, num_iter); for(int i = 0; i < 8 ; i++) { if(hash[i] != hash2[i]) { char s1[65],s2[65]; digest_to_string((u8int*)hash, s1); digest_to_string((u8int*)hash2, s2); printf("Fail!!!\n%s %d\n%s %d\n",s1,completed,s2,c2); exit(0); } }/**/ //hash[7] <= Htarg just compares the first 32 bits of the hash with the target // full_test fully compares the entire hash with the entire target if(hash[7] <= Htarg && fulltest(hash, ptarget)) { *hashes_done = nNonce - pdata[19] + 1 - missed; pdata[19] = block[19]; char s[65]; digest_to_string((u8int*)hash, s); applog(LOG_INFO, "hash found: %s", s); // check uint32_t hash2[8]; int c2 = dcrypt((u8int*)block, 80, digest, hash2, num_iter); for(int i = 0; i < 8 ; i++) { if(hash[i] != hash2[i]) { char s1[65],s2[65]; digest_to_string((u8int*)hash, s1); digest_to_string((u8int*)hash2, s2); printf("Error invalid hash found.\n%s %d\n%s %d\n",s1,completed,s2,c2); exit(0); } } //printf("hash verified.\n"); //found a hash! return 1; } } while(nNonce < max_nonce && !work_restart[thr_id].restart); *hashes_done = nNonce - pdata[19] + 1 - missed; pdata[19] = nNonce; //No luck yet return 0; }