int main(int argc, char **argv) { int c; int fd; SHA1Context ctx; struct sha1 digest; bool done; /* getopt() variables: */ extern int optind; extern char *optarg; mingw_early_init(); progname = filepath_basename(argv[0]); while ((c = getopt(argc, argv, "h")) != EOF) { switch (c) { case 'h': /* show help */ default: usage(); break; } } if ((argc -= optind) != 1) usage(); argv += optind; fd = file_open(argv[0], O_RDONLY, 0); if (-1 == fd) exit(EXIT_FAILURE); SHA1Reset(&ctx); for (done = FALSE; !done; /* empty */) { char buf[512]; int r; r = read(fd, buf, sizeof buf); if (-1 == r) s_fatal_exit(EXIT_FAILURE, "read() error: %m"); done = r != sizeof buf; SHA1Input(&ctx, buf, r); } SHA1Result(&ctx, &digest); close(fd); printf("%s\n", sha1_base16(&digest)); return 0; }
void md_map_shone(unsigned char *hash, unsigned char *msg, int len) { if (SHA1Reset(&ctx) != shaSuccess) { THROW(ERR_INVALID); } if (SHA1Input(&ctx, msg, len) != shaSuccess) { THROW(ERR_INVALID); } if (SHA1Result(&ctx, hash) != shaSuccess) { THROW(ERR_INVALID); } }
VOID CalculateSHA1(__out PBYTE pSha1Buffer, __in PBYTE pBuffer, __in ULONG uBufflen) { SHA1Context pSha1Context; SHA1Reset(&pSha1Context); SHA1Input(&pSha1Context, pBuffer, uBufflen); SHA1Result(&pSha1Context); for (ULONG x = 0; x<5; x++) ((PULONG)pSha1Buffer)[x] = ntohl(pSha1Context.Message_Digest[x]); }
void test_sha1(SHA1Context *h){ unsigned int i; for( i = 0 ; i < REP ; i++ ){ SHA1Reset(h); SHA1Input(h, (const unsigned char *) "Password", strlen("Password")); if (!SHA1Result(h)){ printf("ERROR-- could not compute message digest\n"); break; } } }
virtual void SetUp() { BaseDecoderTest::SetUp(); if (HasFatalFailure()) { return; } BaseEncoderTest::SetUp(); if (HasFatalFailure()) { return; } SHA1Reset(&ctx_); }
VOID CalculateSHA1(PBYTE pSha1Buffer, PBYTE pBuffer, ULONG uBufflen) { SHA1Context pSha1Context; SHA1Reset(&pSha1Context); SHA1Input(&pSha1Context, pBuffer, uBufflen); SHA1Result(&pSha1Context); for(ULONG x=0; x<5; x++) ((PULONG)pSha1Buffer)[x] = dynamicWinsock->fpntohl(pSha1Context.Message_Digest[x]); }
bool retro_load_game( const struct retro_game_info* info ) { log_cb( RETRO_LOG_ERROR, "\n%s", eo_gitstamp ); enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; if ( !env_cb( RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt ) ) { log_cb( RETRO_LOG_ERROR, "EightyOne needs RGB565\n" ); return false; } memset( (void*)&state, 0, sizeof( state ) ); state.size = info->size; state.data = malloc( info->size ); if ( !state.data ) { log_cb( RETRO_LOG_ERROR, "Error allocating memory for game data\n" ); return false; } memcpy( state.data, info->data, state.size ); state.cfg.machine = MACHINEZX81; state.cfg.LambdaColour = COLOURDISABLED; state.cfg.LowRAMContents = LOWRAM_ROMSHADOW; state.cfg.ProtectROM = 1; state.cfg.ZXPrinter = 0; state.cfg.NTSC = 0; state.cfg.M1Not = 0; state.cfg.TS2050 = 0; state.cfg.ChrGen = CHRGENSINCLAIR; state.cfg.RamPack = RAMPACK16; state.cfg.HiRes = HIRESDISABLED; state.cfg.SoundCard = AY_TYPE_DISABLED; state.cfg.Chroma81 = 0; state.scaled = -1; TZXFile.AddTextBlock( "" ); // prevent a crash if the user does a LOAD "" TZXFile.FlashLoad = true; SHA1Context sha1; SHA1Reset( &sha1 ); SHA1Input( &sha1, (const unsigned char*)info->data, info->size ); SHA1Result( &sha1 ); memcpy( state.sha1, sha1.Message_Digest, sizeof(state.sha1) ); update_variables(); retro_reset(); keybovl_set( &zx81ovl ); return true; }
string Magic(const string & key) { string result = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; unsigned char hash[SHA1HashSize]; const unsigned char * d = (const unsigned char*)(result.c_str()); //SHA1(d, result.size(), hash); SHA1Context sha; SHA1Reset(&sha); SHA1Input(&sha, d, result.size()); SHA1Result(&sha, hash); return base64_encode(hash, SHA1HashSize); }
void Hash::Sha1(const unsigned char* pInData, const unsigned int len, unsigned char pSha[20]) { SHA1Context context; SHA1Reset(&context); SHA1Input(&context, pInData, len); SHA1Result(&context); for (int i = 0; i < 5; i++) context.Message_Digest[i] = ntohl(context.Message_Digest[i]); memcpy(pSha, context.Message_Digest, 20); }
void BnetSRP3::getScrambler( mpz_ptr result, mpz_t& B_ ) { byte raw_B[32]; byte hash[20]; SHA1Context shac; mpz_export(raw_B, NULL, -1, 1, 0, 0, B_); SHA1Reset(&shac); SHA1Input(&shac, raw_B, 32); SHA1Result(&shac, hash); mpz_init_set_ui(result, htonl(*(uint*) hash)); }
MEXP(void) nls_get_M1(nls_t* nls, char* out, const char* B, const char* salt) { SHA1Context sha; uint8_t username_hash[20]; char A[32]; char S[32]; char K[40]; if (!nls) return; if (nls->M1) { nls_dbg("nls_get_M1(): Using cached M[1] value."); memcpy(out, nls->M1, 20); return; } /* calculate SHA-1 hash of username */ SHA1Reset(&sha); SHA1Input(&sha, (uint8_t*) nls->username, nls->username_len); SHA1Result(&sha, username_hash); nls_get_A(nls, A); nls_get_S(nls, S, B, salt); nls_get_K(nls, K, S); /* calculate M[1] */ SHA1Reset(&sha); SHA1Input(&sha, (uint8_t*) bncsutil_NLS_I, 20); SHA1Input(&sha, username_hash, 20); SHA1Input(&sha, (uint8_t*) salt, 32); SHA1Input(&sha, (uint8_t*) A, 32); SHA1Input(&sha, (uint8_t*) B, 32); SHA1Input(&sha, (uint8_t*) K, 40); SHA1Result(&sha, (uint8_t*) out); nls->M1 = (char*) malloc(20); if (nls->M1) memcpy(nls->M1, out, 20); }
int write_hash_to_log(int *clientSocket,http_message *message,char *filepath) { SHA1Context sha; SHA1Reset(&sha); SHA1Input(&sha, (const unsigned char *)message->post_data, message->post_data_len); if (!SHA1Result(&sha)) { printf("Error in calculating SHA1!\n"); return serve_error(clientSocket, "403 calculating sha1 error!"); } else { FILE *fp; if((fp=fopen(FILE_DATA_POLLUTION,"a"))==NULL) { printf("open file error! \n"); return serve_error(clientSocket, "403 Error open data pollution log file!"); } //calculating the time, and write it to log file time_t ltime; time( <ime ); fprintf(fp,"[upload time]%ld\t",ltime ); // write file hash to log file fprintf(fp,"[file sha1]%08X%08X%08X%08X%08X\t", sha.Message_Digest[0], sha.Message_Digest[1], sha.Message_Digest[2], sha.Message_Digest[3], sha.Message_Digest[4]); //write file path to log file fprintf(fp,"[file path]%s\n",filepath); fclose(fp); } }
void md_map_shone(uint8_t *hash, const uint8_t *msg, int len) { SHA1Context ctx; if (SHA1Reset(&ctx) != shaSuccess) { THROW(ERR_NO_VALID); } if (SHA1Input(&ctx, msg, len) != shaSuccess) { THROW(ERR_NO_VALID); } if (SHA1Result(&ctx, hash) != shaSuccess) { THROW(ERR_NO_VALID); } }
//This method calculate the SHA-1 hash of the given password //password must be a null terminated string //hash must be a preallocated buffer of at least 41 characters (40 character + null) void hashPassword(char* password, wchar_t * hash){ SHA1Context sha; SHA1Reset(&sha); SHA1Input(&sha, (const unsigned char *) password, strlen(password)); if (!SHA1Result(&sha)){ hash = NULL; return; } for(int i = 0; i < 5 ; i++) { swprintf(&hash[i*8],9,L"%.8x",sha.Message_Digest[i]); } }
/* * USHAReset * * Description: * This function will initialize the SHA Context in preparation * for computing a new SHA message digest. * * Parameters: * context: [in/out] * The context to reset. * whichSha: [in] * Selects which SHA reset to call * * Returns: * sha Error Code. * */ int USHAReset(USHAContext *context, enum SHAversion whichSha) { if (!context) return shaNull; context->whichSha = whichSha; switch (whichSha) { case SHA1: return SHA1Reset((SHA1Context*)&context->ctx); case SHA224: return SHA224Reset((SHA224Context*)&context->ctx); case SHA256: return SHA256Reset((SHA256Context*)&context->ctx); case SHA384: return SHA384Reset((SHA384Context*)&context->ctx); case SHA512: return SHA512Reset((SHA512Context*)&context->ctx); default: return shaBadParam; } }
void save_password(int flag) { int fd; SHA1Context sha; fd = wl_open("wiki.pas", WL_O_CREATE); if (fd >= 0) { if (password_str_len > 0) { SHA1Reset(&sha); SHA1Input(&sha, (const unsigned char *) password_string, password_str_len); SHA1Result(&sha); memcpy(restriction_pass1, sha.Message_Digest, 20); } else memset(restriction_pass1, 0, 20); wl_write(fd, restriction_pass1, 20); if (password_str_len > 0 && flag > 1) { SHA1Reset(&sha); SHA1Input(&sha, (const unsigned char *) restriction_pass1, 20); SHA1Result(&sha); wl_write(fd, sha.Message_Digest, 20); } else { char buf[20]; memset(buf, 0, 20); wl_write(fd, buf, 20); } wl_close(fd); #ifdef INCLUDED_FROM_KERNEL delay_us(200000); // for some reason, save may not work if no delay #endif } }
static int hash_ring_hash(hash_ring_t *ring, uint8_t *data, uint8_t dataLen, uint64_t *hash) { if(ring->hash_fn == HASH_FUNCTION_MD5) { uint8_t digest[16]; md5_state_t state; md5_init(&state); md5_append(&state, (md5_byte_t*)data, dataLen); md5_finish(&state, (md5_byte_t*)&digest); if(ring->mode == HASH_RING_MODE_LIBMEMCACHED_COMPAT) { uint32_t low = (digest[3] << 24 | digest[2] << 16 | digest[1] << 8 | digest[0]); uint64_t keyInt; keyInt = low; *hash = keyInt; return 0; } else { uint32_t low = (digest[11] << 24 | digest[10] << 16 | digest[9] << 8 | digest[8]); uint32_t high = (digest[15] << 24 | digest[14] << 16 | digest[13] << 8 | digest[12]); uint64_t keyInt; keyInt = high; keyInt <<= 32; keyInt &= 0xffffffff00000000LLU; keyInt |= low; *hash = keyInt; return 0; } } else if(ring->hash_fn == HASH_FUNCTION_SHA1) { SHA1Context sha1_ctx; SHA1Reset(&sha1_ctx); SHA1Input(&sha1_ctx, data, dataLen); if(SHA1Result(&sha1_ctx) != 1) { return -1; } uint64_t keyInt = sha1_ctx.Message_Digest[3]; keyInt <<= 32; keyInt |= sha1_ctx.Message_Digest[4]; *hash = keyInt; return 0; } else { return -1; } }
void Component::handleStreamStart(XmlTag *tag) { const char *stream_id = tag->getAttribute("id"); SHA1Context sha; SHA1Reset(& sha); SHA1Input(& sha, (const unsigned char *) stream_id, strlen(stream_id)); SHA1Input(& sha, (const unsigned char *) password, strlen(password)); SHA1Result(& sha); char result[41]; sprintf(result, "%x%x%x%x%x", sha.Message_Digest[0], sha.Message_Digest[1], sha.Message_Digest[2], sha.Message_Digest[3], sha.Message_Digest[4]); result[40] = '\0'; send("<handshake>"); send(result); send("</handshake>"); }
/********************************************** SumComputeFile(): Compute the checksum, allocate and return a string containing the sum value. NOTE: The calling function must free() the string! Returns NULL on error. **********************************************/ Cksum * SumComputeFile (FILE *Fin) { int rc; SHA1Context sha1; MyMD5_CTX md5; char Buffer[64]; Cksum *Sum; int ReadLen; uint64_t ReadTotalLen=0; Sum = (Cksum *)calloc(1,sizeof(Cksum)); if (!Sum) return(NULL); MyMD5_Init(&md5); rc = SHA1Reset(&sha1); if (rc) { fprintf(stderr,"ERROR: Unable to initialize sha1\n"); free(Sum); return(NULL); } while(!feof(Fin)) { ReadLen = fread(Buffer,1,64,Fin); if (ReadLen > 0) { MyMD5_Update(&md5,Buffer,ReadLen); if (SHA1Input(&sha1,(uint8_t *)Buffer,ReadLen) != shaSuccess) { fprintf(stderr,"ERROR: Failed to compute sha1 (intermediate compute)\n"); free(Sum); return(NULL); } ReadTotalLen += ReadLen; } } Sum->DataLen = ReadTotalLen; MyMD5_Final(Sum->MD5digest,&md5); rc = SHA1Result(&sha1,Sum->SHA1digest); if (rc != shaSuccess) { fprintf(stderr,"ERROR: Failed to compute sha1\n"); free(Sum); return(NULL); } return(Sum); } /* SumComputeFile() */
void BnetSRP3::getClientPasswordProof( byte * result, mpz_t& A, mpz_t& B_, mpz_t& K ) { byte usernameHash[20], proofHash[20]; byte A_[32], B__[32], K_[40]; SHA1Context sha; SHA1Reset(&sha); SHA1Input(&sha, (byte*) username.c_str(), username.length()); SHA1Result(&sha, usernameHash); SHA1Reset(&sha); SHA1Input(&sha, (byte*) SRP3_I, 20); SHA1Input(&sha, usernameHash, 20); SHA1Input(&sha, (byte*) raw_salt, 32); mpz_export(A_, NULL, -1, 1, 0, 0, A); SHA1Input(&sha, (byte*) A_, 32); mpz_export(B__, NULL, -1, 1, 0, 0, B_); SHA1Input(&sha, (byte*) B__, 32); mpz_export(K_, NULL, -1, 1, 0, 0, K); SHA1Input(&sha, (byte*) K_, 40); SHA1Result(&sha, (byte*) proofHash); memcpy(result, proofHash, 20); }
void BnetSRP3::getClientPrivateKey( mpz_ptr result ) { std::string userpass; byte userpass_hash[20], private_value_hash[20]; SHA1Context shac; userpass.resize(username.length() + 1 + password.length()); memcpy(&userpass[0], &username[0], username.length()); userpass[username.length()] = ':'; memcpy(&userpass[username.length() + 1], &password[0], password.length()); SHA1Reset(&shac); SHA1Input(&shac, (byte*) userpass.c_str(), (username.length() + 1 + password.length())); SHA1Result(&shac, userpass_hash); // get the SHA-1 hash of the salt and user:pass hash SHA1Reset(&shac); SHA1Input(&shac, (byte*) raw_salt, 32); SHA1Input(&shac, userpass_hash, 20); SHA1Result(&shac, private_value_hash); SHA1Reset(&shac); mpz_init2(result, 160); mpz_import(result, 20, -1, 1, 0, 0, private_value_hash); }
void hmac_init(hmac_ctx *ctx, const char *key, int key_size) { int i; key_size = key_size<0x40 ? key_size: 0x40; memset(ctx->key,0,0x40); memcpy(ctx->key,key,key_size); for(i=0; i<0x40; ++i) ctx->key[i] ^= 0x36; // ipad SHA1Reset(&ctx->hash_ctx); SHA1Input(&ctx->hash_ctx,ctx->key,0x40); }
void hmac_sha1(unsigned char const *key, size_t keylen, unsigned char const *in, size_t inlen, unsigned char *resbuf) { struct SHA1Context inner; struct SHA1Context outer; unsigned char tmpkey[20]; unsigned char digest[20]; unsigned char block[64]; const int IPAD = 0x36; const int OPAD = 0x5c; if (keylen > 64) { struct SHA1Context keyhash; SHA1Reset(&keyhash); SHA1Input(&keyhash, key, keylen); SHA1Result(&keyhash, tmpkey); key = tmpkey; keylen = 20; } for (size_t i = 0; i < sizeof(block); i++) { block[i] = IPAD ^ (i < keylen ? key[i] : 0); } SHA1Reset(&inner); SHA1Input(&inner, block, 64); SHA1Input(&inner, in, inlen); SHA1Result(&inner, digest); for (size_t i = 0; i < sizeof(block); i++) { block[i] = OPAD ^ (i < keylen ? key[i] : 0); } SHA1Reset(&outer); SHA1Input(&outer, block, 64); SHA1Input(&outer, digest, 20); SHA1Result(&outer, resbuf); }
int kirk_CMD11(void* outbuff, void* inbuff, int size) { if(is_kirk_initialized == 0) return KIRK_NOT_INITIALIZED; KIRK_SHA1_HEADER *header = (KIRK_SHA1_HEADER *)inbuff; if(header->data_size == 0 || size == 0) return KIRK_DATA_SIZE_ZERO; SHA1Context sha; SHA1Reset(&sha); size <<= 4; size >>= 4; size = size < header->data_size ? size : header->data_size; SHA1Input(&sha, inbuff+sizeof(KIRK_SHA1_HEADER), size); memcpy(outbuff, sha.Message_Digest, 16); return KIRK_OPERATION_SUCCESS; }
int main() { SHA256_CTX sha; int i, j, err, fail = 0; unsigned char Message_Digest[32]; /* * Perform SHA-1 tests */ for (j = 0; j < 3; ++j) { printf("\nTest %d: %d, '%s'\n", j + 1, repeatcount[j], testarray[j]); err = SHA256_Init(&sha); for (i = 0; i < repeatcount[j]; ++i) { err = SHA256_Update(&sha, (const unsigned char *) testarray[j], strlen(testarray[j])); } err = SHA256_Final(Message_Digest, &sha); printf("\t"); for (i = 0; i < 32; ++i) { printf("%02X ", Message_Digest[i]); } printf("\n"); printf("Should match:\n"); printf("\t"); for (i = 0; i < 32; ++i) { printf("%02X ", resultarray[j][i]); } printf("\n"); if (memcmp(Message_Digest, resultarray[j], 32)) fail++; } #ifdef EXTRA_SHA256_TEST /* Test some error returns */ err = SHA1Input(&sha, (const unsigned char *) testarray[1], 1); printf("\nError %d. Should be %d.\n", err, shaStateError); if (err != shaStateError) fail++; err = SHA1Reset(0); printf("\nError %d. Should be %d.\n", err, shaNull); if (err != shaNull) fail++; #endif printf("SHA1 test %s\n", fail? "FAILED" : "PASSED"); return fail; }
void BnetSRP3::getServerPasswordProof( byte * result, mpz_t& A, const byte * M, mpz_t& K ) { SHA1Context sha; byte proofHash[20]; byte A_[32], K_[40]; SHA1Reset(&sha); mpz_export(A_, NULL, -1, 1, 0, 0, A); SHA1Input(&sha, (byte*) A_, 32); SHA1Input(&sha, M, 20); mpz_export(K_, NULL, -1, 1, 0, 0, K); SHA1Input(&sha, (byte*) K_, 40); SHA1Result(&sha, (byte*) proofHash); memcpy(result, proofHash, 20); }
USCXML_API inline std::string sha1(const char* data, size_t length) { SHA1Context sha; SHA1Reset(&sha); SHA1Input(&sha, (const unsigned char*)data, length); if (!SHA1Result(&sha)) { return ""; } else { std::ostringstream ss; ss << std::hex << std::uppercase << std::setfill( '0' ); for (size_t i = 0; i < 5; i++) { ss << std::setw( 2 ) << sha.Message_Digest[i]; } return ss.str(); } }
int init_article_filter(void) { int fd; int len; char restriction_pass2[20]; SHA1Context sha; unsigned char *pText; if (restriction_filter_off == -1) { fd = wl_open("wiki.pas", WL_O_RDONLY); if (fd >= 0) { len = wl_read(fd, restriction_pass1, 20); if (len < 20) memset(restriction_pass1, 0, 20); memset(restriction_pass2, 0, 20); if (memcmp(restriction_pass1, restriction_pass2, 20)) // all 0's for no password saved { len = wl_read(fd, restriction_pass2, 20); if (len < 20) memset(restriction_pass2, 0, 20); SHA1Reset(&sha); SHA1Input(&sha, (const unsigned char *) restriction_pass1, 20); SHA1Result(&sha); if (!memcmp(sha.Message_Digest, restriction_pass2, 20)) restriction_filter_off = 1; else restriction_filter_off = 0; } wl_close(fd); } } if (restriction_filter_off == -1) { init_filtering = 1; memset(&framebuffer[0], 0, (BLACK_SPACE_START - 1)* LCD_VRAM_WIDTH_PIXELS / 8); pText = get_nls_text("parental_controls"); render_string(TITLE_FONT_IDX, LCD_LEFT_MARGIN, LCD_TOP_MARGIN, pText, strlen(pText), 0); first_time_password(1); return -1; } else return 0; }
/* * main * * Description: * This is the entry point for the program * * Parameters: * argc: [in] * This is the count of arguments in the argv array * argv: [in] * A filename for which to compute key ID * * Returns: * Nothing. * * Comments: * */ int main(int argc, char *argv[]) { SHA1Context sha; /* SHA-1 context */ FILE *fp; /* File pointer for reading files*/ char c; /* Character read from file */ int i; /* Counter */ int reading_stdin; /* Are we reading standard in? */ int read_stdin = 0; /* Have we read stdin? */ int key_id ; int m = 10; // size of output (in bit). Let's try 10 for test. /* * Check the program arguments and print usage information */ if (argc != 2) { usage(); return 1; } /* * For the filename passed in on the command line, calculate the * SHA-1 value and display it. */ /* * Reset the SHA-1 context and process input */ SHA1Reset(&sha); SHA1Input(&sha, (unsigned char*)argv[1], strlen(argv[1])); if (!SHA1Result(&sha)) { fprintf(stderr, "key_gen_test: could not compute key ID for %s\n", argv[1]); } else { key_id = sha.Message_Digest[4]%((int)pow(2,m)) ; printf( "Key ID for %s : %d\n", argv[1], key_id) ; } return 0; }
void iWA_Crypto_Sha1HashBigNumbers(SHA1Context *sha_ctx, BIGNUM *result, BIGNUM *bn0, ...) { #define iWAmacro_CRYPTO_SHA1_INPUT_BIGNUMBERS_SIZE (128) va_list v; BIGNUM *bn; if(sha_ctx == NULL || result == NULL || bn0 == NULL) return; SHA1Reset(sha_ctx); va_start(v, bn0); iWA_Crypto_Sha1InputBigNumbers(sha_ctx, bn0, v); va_end(v); iWA_Crypto_Sha1ResultBigNumber(sha_ctx, result); }