static mrb_value mrb_digest_hmac_hexdigest(mrb_state *mrb, mrb_value self) { mrb_digest *digest; void *ctx_tmp; unsigned char md[MRB_DIGEST_AVAILABLE_SIZ]; size_t md_len; char hex[MRB_HEXDIGEST_AVAILABLE_SIZ]; digest = mrb_get_datatype(mrb, self, &mrb_hmac_type); if (digest == NULL) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } ctx_tmp = malloc(digest->ctx_size); if (ctx_tmp == NULL) { mrb_raise(mrb, E_RUNTIME_ERROR, "cannot allocate memory"); } memcpy(ctx_tmp, digest->ctx, digest->ctx_size); md_len = 0L; call_hmac_final(mrb, FFI_FN(digest->func_final), ctx_tmp, md, &md_len); free(ctx_tmp); return mrb_str_new(mrb, (const char *)digest2hex(hex, md, md_len), md_len * 2); }
void test_gridfile(gridfs *gfs, char *data_before, uint64_t length, char *filename, char *content_type) { gridfile gfile[1]; char *data_after = malloc( LARGE ); if( data_after == NULL ) { printf("Failed to allocated memory"); exit(1); } FILE * fd; mongo_md5_state_t pms[1]; mongo_md5_byte_t digest[16]; char hex_digest[33]; uint64_t i = length; int n; gridfs_find_filename(gfs, filename, gfile); ASSERT(gridfile_exists(gfile)); fd = fopen("output", "w+"); gridfile_write_file(gfile, fd); fseek(fd, 0, SEEK_SET); ASSERT(fread(data_after, length, sizeof(char), fd)); fclose(fd); ASSERT( strncmp(data_before, data_after, length) == 0 ); gridfile_read( gfile, length, data_after ); ASSERT( strncmp(data_before, data_after, length) == 0 ); ASSERT( strcmp( gridfile_get_filename( gfile ), filename ) == 0 ); ASSERT( gridfile_get_contentlength( gfile ) == length ); ASSERT( gridfile_get_chunksize( gfile ) == DEFAULT_CHUNK_SIZE ); ASSERT( strcmp( gridfile_get_contenttype( gfile ), content_type ) == 0) ; ASSERT( strncmp( data_before, data_after, length ) == 0 ); mongo_md5_init(pms); n = 0; while( i > INT_MAX ) { mongo_md5_append(pms, (const mongo_md5_byte_t *)data_before + (n * INT_MAX), INT_MAX); i -= INT_MAX; n += 1; } if( i > 0 ) mongo_md5_append(pms, (const mongo_md5_byte_t *)data_before + (n * INT_MAX), i); mongo_md5_finish(pms, digest); digest2hex(digest, hex_digest); ASSERT( strcmp( gridfile_get_md5( gfile ), hex_digest ) == 0 ); gridfile_destroy(gfile); gridfs_remove_filename(gfs, filename); free( data_after ); }
static void mongo_pass_digest( const char *user, const char *pass, char hex_digest[33] ) { mongo_md5_state_t st; mongo_md5_byte_t digest[16]; mongo_md5_init( &st ); mongo_md5_append( &st, ( const mongo_md5_byte_t * )user, strlen( user ) ); mongo_md5_append( &st, ( const mongo_md5_byte_t * )":mongo:", 7 ); mongo_md5_append( &st, ( const mongo_md5_byte_t * )pass, strlen( pass ) ); mongo_md5_finish( &st, digest ); digest2hex( digest, hex_digest ); }
static int format_hex(const uint8_t *p, size_t plen, off_t flen, const char *path) { char *hexdigest = alloca(2*plen + 1); (void)flen; digest2hex(hexdigest, p, plen); fputs(hexdigest, stdout); fputs(" ", stdout); if (*path == '\0') path = "-"; puts(path); return 0; }
int main(int argc, char *argv[]) { char hash[HASH_LEN]; if (argc < 3) { printf("Usage: %s hash_file rainbow_file\n", argv[0]); exit(1); } // Open both hash and rainbow.bow file for reading FILE *hf = fopen(argv[1], "r"); if (!hf) { printf("Hash file could not be opened!\n"); exit(1); } FILE *rf = fopen(argv[2], "rb"); if (!rf) { printf("Binary file could not be opened!\n"); exit(1); } // Takes the hash file and hashes each value // Seeks to the appropriate mem location and returns password found while ( fscanf(hf, "%s\n", hash) != EOF ) { entry e; int file_location = locate_file(hash); if (fseek(rf, file_location, SEEK_SET) < 0) { P("Error! %s\n", strerror(errno)); } // Loops while password is not found while(fread(&e, sizeof(entry), 1, rf) == 1) { // Convert the raw data to hex char *new_hash = digest2hex(e.hash); // Compare raw hashes, if equal, print // the hash and plain text password, break if (memcmp(new_hash, hash, 16) == 0) { P("%s %s\n", hash, e.pass); break; } } } fclose(rf); fclose(hf); }
static int format_hexlen(const uint8_t *p, size_t plen, off_t flen, const char *path) { char *hexdigest = alloca(2*plen + 1), buf[12] = " \0"; digest2hex(hexdigest, p, plen); fputs(hexdigest, stdout); putc(' ', stdout); int2str(buf, flen); fputs(buf, stdout); if (*path == '\0') path = "-"; puts(path); return 0; }
bson_bool_t mongo_cmd_authenticate( mongo *conn, const char *db, const char *user, const char *pass ) { bson from_db; bson cmd; bson out; const char *nonce; bson_bool_t success = 0; mongo_md5_state_t st; mongo_md5_byte_t digest[16]; char hex_digest[33]; if( mongo_simple_int_command( conn, db, "getnonce", 1, &from_db ) == MONGO_OK ) { bson_iterator it; bson_find( &it, &from_db, "nonce" ); nonce = bson_iterator_string( &it ); } else { return MONGO_ERROR; } mongo_pass_digest( user, pass, hex_digest ); mongo_md5_init( &st ); mongo_md5_append( &st, ( const mongo_md5_byte_t * )nonce, strlen( nonce ) ); mongo_md5_append( &st, ( const mongo_md5_byte_t * )user, strlen( user ) ); mongo_md5_append( &st, ( const mongo_md5_byte_t * )hex_digest, 32 ); mongo_md5_finish( &st, digest ); digest2hex( digest, hex_digest ); bson_init( &cmd ); bson_append_int( &cmd, "authenticate", 1 ); bson_append_string( &cmd, "user", user ); bson_append_string( &cmd, "nonce", nonce ); bson_append_string( &cmd, "key", hex_digest ); bson_finish( &cmd ); bson_destroy( &from_db ); /*bson_init( &from_db ); */ if( mongo_run_command( conn, db, &cmd, &out ) == MONGO_OK ) { bson_iterator it; if( bson_find( &it, &out, "ok" ) ) success = bson_iterator_bool( &it ); } bson_destroy( &from_db ); bson_destroy( &cmd ); if( success ) return MONGO_OK; else return MONGO_ERROR; }
void test_gridfile(gridfs *gfs, char *data_before, size_t length, char *filename, char *content_type) { gridfile gfile[1]; char data_after[UPPER]; FILE * fd; mongo_md5_state_t pms[1]; mongo_md5_byte_t digest[16]; char hex_digest[33]; gridfs_find_filename(gfs, filename, gfile); ASSERT(gridfile_exists(gfile)); fd = fopen("output", "w+"); gridfile_write_file(gfile, fd); fseek(fd, 0, SEEK_SET); ASSERT(fread(data_after, length, sizeof(char), fd)); fclose(fd); ASSERT( strncmp(data_before, data_after, length) == 0 ); gridfile_read( gfile, length, data_after); ASSERT( strncmp(data_before, data_after, length) == 0 ); ASSERT( strcmp( gridfile_get_filename( gfile ), filename ) == 0 ); ASSERT( gridfile_get_contentlength( gfile ) == length ); ASSERT( gridfile_get_chunksize( gfile ) == DEFAULT_CHUNK_SIZE ); ASSERT( strcmp( gridfile_get_contenttype( gfile ), content_type ) == 0) ; mongo_md5_init(pms); mongo_md5_append(pms, (const mongo_md5_byte_t *)data_before, (int)length); mongo_md5_finish(pms, digest); digest2hex(digest, hex_digest); ASSERT( strcmp( gridfile_get_md5( gfile ), hex_digest ) == 0 ); gridfile_destroy(gfile); gridfs_remove_filename(gfs, filename); }
static void test_gridfile( gridfs *gfs, char *data_before, int64_t length, char *filename, char *content_type ) { gridfile gfile[1]; FILE *stream; #ifdef DYING mongo_md5_state_t pms[1]; mongo_md5_byte_t digest[16]; #endif char hex_digest[33]; int64_t i = length; int n; char *data_after = (char*)bson_malloc( LARGE ); int truncBytes; char* lowerName; ASSERT(gridfs_find_filename( gfs, filename, gfile ) == MONGO_OK); ASSERT( gridfile_exists( gfile ) ); stream = fopen( "output", "w+" ); gridfile_write_file( gfile, stream ); fseek( stream, 0, SEEK_SET ); ASSERT( fread( data_after, (size_t)length, sizeof( char ), stream ) ); fclose( stream ); ASSERT( memcmp( data_before, data_after, (size_t)length ) == 0 ); gridfile_read_buffer( gfile, data_after, length ); ASSERT( memcmp( data_before, data_after, (size_t)length ) == 0 ); lowerName = (char*) bson_malloc( (int)strlen( filename ) + 1); strcpy( lowerName, filename ); _strlwr( lowerName ); ASSERT( strcmp( gridfile_get_filename( gfile ), lowerName ) == 0 ); bson_free( lowerName ); ASSERT( gridfile_get_contentlength( gfile ) == (size_t)length ); ASSERT( gridfile_get_chunksize( gfile ) == DEFAULT_CHUNK_SIZE ); ASSERT( strcmp( gridfile_get_contenttype( gfile ), content_type ) == 0 ) ; ASSERT( memcmp( data_before, data_after, (size_t)length ) == 0 ); if( !( gfile->flags & GRIDFILE_COMPRESS ) ) { #ifdef DYING mongo_md5_init( pms ); n = 0; while( i > INT_MAX ) { mongo_md5_append( pms, ( const mongo_md5_byte_t * )data_before + ( n * INT_MAX ), INT_MAX ); i -= INT_MAX; n += 1; } if( i > 0 ) mongo_md5_append( pms, ( const mongo_md5_byte_t * )data_before + ( n * INT_MAX ), (int)i ); mongo_md5_finish( pms, digest ); digest2hex( digest, hex_digest ); #else { DIGEST_CTX ctx = rpmDigestInit(PGPHASHALGO_MD5, RPMDIGEST_NONE); const char * _digest = NULL; int xx; while( i > INT_MAX ) { xx = rpmDigestUpdate(ctx, (char *)data_before + (n*INT_MAX), INT_MAX); i -= INT_MAX; n += 1; } xx = rpmDigestFinal(ctx, &_digest, NULL, 1); strncpy(hex_digest, _digest, 32+1); hex_digest[32] = '\0'; _digest = _free(_digest); } #endif ASSERT( strcmp( gridfile_get_md5( gfile ), hex_digest ) == 0 ); } truncBytes = (int) (length > DEFAULT_CHUNK_SIZE * 4 ? length - DEFAULT_CHUNK_SIZE * 2 - 13 : 23); gridfile_writer_init( gfile, gfs, filename, content_type, GRIDFILE_DEFAULT); ASSERT( gridfile_truncate(gfile, (size_t)(length - truncBytes)) == (size_t)(length - truncBytes)); gridfile_writer_done( gfile ); gridfile_seek(gfile, 0); ASSERT( gridfile_get_contentlength( gfile ) == (size_t)(length - truncBytes) ); ASSERT( gridfile_read_buffer( gfile, data_after, length ) == (size_t)(length - truncBytes)); ASSERT( memcmp( data_before, data_after, (size_t)(length - truncBytes) ) == 0 ); gridfile_writer_init( gfile, gfs, filename, content_type, GRIDFILE_DEFAULT); gridfile_truncate(gfile, 0); gridfile_writer_done( gfile ); ASSERT( gridfile_get_contentlength( gfile ) == 0 ); ASSERT( gridfile_read_buffer( gfile, data_after, length ) == 0 ); gridfile_destroy( gfile ); ASSERT( gridfs_remove_filename( gfs, filename ) == MONGO_OK ); free( data_after ); gridfs_test_unlink( "output" ); }
void test_gridfile( gridfs *gfs, char *data_before, int64_t length, char *filename, char *content_type ) { gridfile gfile[1]; FILE *stream; mongo_md5_state_t pms[1]; mongo_md5_byte_t digest[16]; char hex_digest[33]; int64_t i = length; int n; char *data_after = (char*)bson_malloc( LARGE ); int truncBytes; char* lowerName; ASSERT(gridfs_find_filename( gfs, filename, gfile ) == MONGO_OK); ASSERT( gridfile_exists( gfile ) ); stream = fopen( "output", "w+" ); gridfile_write_file( gfile, stream ); fseek( stream, 0, SEEK_SET ); ASSERT( fread( data_after, (size_t)length, sizeof( char ), stream ) ); fclose( stream ); ASSERT( memcmp( data_before, data_after, (size_t)length ) == 0 ); gridfile_read( gfile, length, data_after ); ASSERT( memcmp( data_before, data_after, (size_t)length ) == 0 ); lowerName = (char*) bson_malloc( (int)strlen( filename ) + 1); strcpy( lowerName, filename); _strlwr( lowerName ); ASSERT( strcmp( gridfile_get_filename( gfile ), lowerName ) == 0 ); bson_free( lowerName ); ASSERT( gridfile_get_contentlength( gfile ) == (size_t)length ); ASSERT( gridfile_get_chunksize( gfile ) == DEFAULT_CHUNK_SIZE ); ASSERT( strcmp( gridfile_get_contenttype( gfile ), content_type ) == 0 ) ; ASSERT( memcmp( data_before, data_after, (size_t)length ) == 0 ); if( !( gfile->flags & GRIDFILE_COMPRESS ) ) { mongo_md5_init( pms ); n = 0; while( i > INT_MAX ) { mongo_md5_append( pms, ( const mongo_md5_byte_t * )data_before + ( n * INT_MAX ), INT_MAX ); i -= INT_MAX; n += 1; } if( i > 0 ) mongo_md5_append( pms, ( const mongo_md5_byte_t * )data_before + ( n * INT_MAX ), (int)i ); mongo_md5_finish( pms, digest ); digest2hex( digest, hex_digest ); ASSERT( strcmp( gridfile_get_md5( gfile ), hex_digest ) == 0 ); } truncBytes = (int) (length > DEFAULT_CHUNK_SIZE * 4 ? length - DEFAULT_CHUNK_SIZE * 2 - 13 : 23); gridfile_writer_init( gfile, gfs, filename, content_type, GRIDFILE_DEFAULT); ASSERT( gridfile_truncate(gfile, (size_t)(length - truncBytes)) == (size_t)(length - truncBytes)); gridfile_writer_done( gfile ); gridfile_seek(gfile, 0); ASSERT( gridfile_get_contentlength( gfile ) == (size_t)(length - truncBytes) ); ASSERT( gridfile_read( gfile, length, data_after ) == (size_t)(length - truncBytes)); ASSERT( memcmp( data_before, data_after, (size_t)(length - truncBytes) ) == 0 ); gridfile_writer_init( gfile, gfs, filename, content_type, GRIDFILE_DEFAULT); gridfile_truncate(gfile, 0); gridfile_writer_done( gfile ); ASSERT( gridfile_get_contentlength( gfile ) == 0 ); ASSERT( gridfile_read( gfile, length, data_after ) == 0 ); gridfile_destroy( gfile ); gridfs_remove_filename( gfs, filename ); free( data_after ); _unlink( "output" ); }