/** * @deprecated Use ssh_get_publickey_hash() */ int ssh_get_pubkey_hash(ssh_session session, unsigned char **hash) { ssh_string pubkey; MD5CTX ctx; unsigned char *h; if (session == NULL || hash == NULL) { return SSH_ERROR; } *hash = NULL; if (session->current_crypto == NULL || session->current_crypto->server_pubkey == NULL){ ssh_set_error(session,SSH_FATAL,"No current cryptographic context"); return SSH_ERROR; } h = malloc(sizeof(unsigned char) * MD5_DIGEST_LEN); if (h == NULL) { return SSH_ERROR; } ctx = md5_init(); if (ctx == NULL) { SAFE_FREE(h); return SSH_ERROR; } pubkey = session->current_crypto->server_pubkey; md5_update(ctx, ssh_string_data(pubkey), ssh_string_len(pubkey)); md5_final(h, ctx); *hash = h; return MD5_DIGEST_LEN; }
int dsync_mail_get_hdr_hash(struct mail *mail, const char **hdr_hash_r) { struct istream *hdr_input, *input; struct mailbox_header_lookup_ctx *hdr_ctx; struct md5_context md5_ctx; unsigned char md5_result[MD5_RESULTLEN]; const unsigned char *data; size_t size; int ret = 0; hdr_ctx = mailbox_header_lookup_init(mail->box, hashed_headers); ret = mail_get_header_stream(mail, hdr_ctx, &hdr_input); mailbox_header_lookup_unref(&hdr_ctx); if (ret < 0) return -1; input = i_stream_create_lf(hdr_input); md5_init(&md5_ctx); while (!i_stream_is_eof(input)) { if (i_stream_read_data(input, &data, &size, 0) == -1) break; if (size == 0) break; md5_update(&md5_ctx, data, size); i_stream_skip(input, size); } if (input->stream_errno != 0) ret = -1; i_stream_unref(&input); md5_final(&md5_ctx, md5_result); *hdr_hash_r = binary_to_hex(md5_result, sizeof(md5_result)); return ret; }
static int build_session_id1(ssh_session session, ssh_string servern, ssh_string hostn) { MD5CTX md5 = NULL; md5 = md5_init(); if (md5 == NULL) { return -1; } #ifdef DEBUG_CRYPTO ssh_print_hexa("host modulus",ssh_string_data(hostn),ssh_string_len(hostn)); ssh_print_hexa("server modulus",ssh_string_data(servern),ssh_string_len(servern)); #endif md5_update(md5,ssh_string_data(hostn),ssh_string_len(hostn)); md5_update(md5,ssh_string_data(servern),ssh_string_len(servern)); md5_update(md5,session->next_crypto->server_kex.cookie,8); if(session->next_crypto->session_id != NULL) SAFE_FREE(session->next_crypto->session_id); session->next_crypto->session_id = malloc(MD5_DIGEST_LEN); if(session->next_crypto->session_id == NULL){ ssh_set_error_oom(session); return SSH_ERROR; } md5_final(session->next_crypto->session_id,md5); #ifdef DEBUG_CRYPTO ssh_print_hexa("session_id",session->next_crypto->session_id,MD5_DIGEST_LEN); #endif return 0; }
int md5hex(const char *fname, char *hex_digest) { int bsize = 1024; unsigned char buffer[bsize], digest[16]; size_t len; MD5_CONTEXT context; FILE *fh = fopen(fname, "rb"); if (fh == NULL) { strcpy(hex_digest, ZERO_MD5); return 0; } struct stat st; exit_unless(stat(fname, &st) == 0 && S_ISREG(st.st_mode), "%s is not a file\n", fname); md5_init(&context); while ((len = fread(buffer, 1, bsize, fh)) > 0) { md5_write(&context, buffer, len); } md5_final(&context); memcpy(digest, context.buf, 16); exit_if(-1 == fclose(fh), "Error closing stream: %s", strerror(errno)); for (int i = 0; i < 16; i++) { snprintf(&hex_digest[2 * i], 3, "%02x", digest[i]); } return 1; }
char *calculate_checksum(char *file) { FILE *fp; int bytes, i; md5_t ctx; unsigned char data[1024]; unsigned char c[MD5_DIGEST_LENGTH]; char *md5 = malloc(MD5_DIGEST_LENGTH *2 + 1); fp = fopen(file, "rb"); if (!fp) { return NULL; } md5_init(&ctx); while ((bytes = fread(data, 1, 1024, fp)) != 0) { md5_update(&ctx, data, bytes); } md5_final(c, &ctx); for (i = 0; i < MD5_DIGEST_LENGTH; ++i) { snprintf(&md5[i*2], MD5_DIGEST_LENGTH*2, "%02x", c[i]); } fclose(fp); return md5; }
int md5_file(const char *filename){ int len,i; unsigned char buf[1024]; unsigned char output[16]; int fd=open(filename,O_RDONLY); CONTEXT cxt; if(fd<0){ perror("open file"); exit(0); } md5_init(&cxt); while((len=read(fd,buf,1024))>0){ md5_update(&cxt,buf,len); } md5_final(&cxt,output); close(fd); //output for(i=0;i<16;i++) printf("%02x",output[i]); printf("\n"); return 0; }
static int passphrase_to_key(char *data, unsigned int datalen, unsigned char *salt, unsigned char *key, unsigned int keylen) { MD5CTX md; unsigned char digest[MD5_DIGEST_LEN] = {0}; unsigned int i; unsigned int j; unsigned int md_not_empty; for (j = 0, md_not_empty = 0; j < keylen; ) { md = md5_init(); if (md == NULL) { return -1; } if (md_not_empty) { md5_update(md, digest, MD5_DIGEST_LEN); } else { md_not_empty = 1; } md5_update(md, data, datalen); if (salt) { md5_update(md, salt, PKCS5_SALT_LEN); } md5_final(digest, md); for (i = 0; j < keylen && i < MD5_DIGEST_LEN; j++, i++) { if (key) { key[j] = digest[i]; } } } return 0; }
void http_auth_headers(void *ctx, char *user, char *pass) { char *tmp; int i; unsigned char hash[16]; unsigned int m; struct md5_context md5; if(user) { http_async_add_header(ctx, "X-Auth-User", user); if(pass) { md5_init(&md5); md5_update(&md5, (unsigned char *)user, strlen(user)); md5_update(&md5, (unsigned char *)"-", 1); m = 0; md5_update(&md5, (unsigned char *)pass, strlen(pass)); md5_final(hash, &md5); tmp = malloc(33); for(i=0;i<16;i++) { tmp[i*2] = hex[hash[i]>>4]; tmp[i*2+1] = hex[hash[i]&15]; } tmp[32] = 0; http_async_add_header(ctx, "X-Auth-Hash", tmp); free(tmp); } } }
int cmd_md5sum(int argc, char **argv) { int fd; int ret; char buf[512]; unsigned char *p; int i; if (argc<2) return -1; md5_init(); fd = open(argv[1], O_RDONLY); while(1) { ret = read(fd, buf, 512); if (ret <= 0) break; md5_update(buf, ret); } close(fd); p= md5_final(); for (i=0; i<16; i++) printf("%02x", p[i]); printf("\n"); return 0; }
static void MDTimeTrial (void) { MD5_CONTEXT context; time_t endTime, startTime; unsigned char block [TEST_BLOCK_LEN], digest [16]; unsigned i; printf ("MD5 time trial. Digesting %d %d-byte blocks ...", TEST_BLOCK_COUNT, TEST_BLOCK_LEN); /* Initialize block */ for (i = 0; i < TEST_BLOCK_LEN; i++) block [i] = (unsigned char)(i & 0xff); /* Start timer */ time (&startTime); /* Digest blocks */ md5_init (&context); for (i = 0; i < TEST_BLOCK_COUNT; i++) md5_update (&context, block, TEST_BLOCK_LEN); md5_final (digest, &context); /* Stop timer */ time (&endTime); printf (" done\n"); printf ("Digest = "); MDPrint (digest); printf ("\nTime = %ld seconds\n", (long) (endTime - startTime)); printf ("Speed = %ld bytes/second\n", (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / (endTime - startTime)); }
static char * md5 (const char *input) { memcpy ((char *) state, (char *) initstate, sizeof (initstate)); length = 0; md5_update (input, strlen (input)); return md5_final (); }
static void calculate_hash(char *str, hash_digest_t sig) { struct md5_context md5c; md5_init(&md5c); md5_update(&md5c, (u8 *)str, strlen(str)); md5_final(sig, &md5c); }
static char * libshadow_md5_crypt(const char *pw, const char *salt) { static char *magic = "$1$"; /* * This string is magic for * this algorithm. Having * it this way, we can get * get better later on */ static char passwd[120], *p; static const char *sp,*ep; unsigned char final[16]; int sl,pl,i,j; md5_context ctx,ctx1; unsigned long l; /* Refine the Salt first */ sp = salt; /* If it starts with the magic string, then skip that */ if(!strncmp(sp,magic,strlen(magic))) sp += strlen(magic); /* It stops at the first '$', max 8 chars */ for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) continue; /* get the length of the true salt */ sl = ep - sp; md5_init(&ctx); /* The password first, since that is what is most unknown */ md5_update(&ctx,pw,strlen(pw)); /* Then our magic string */ md5_update(&ctx,magic,strlen(magic)); /* Then the raw salt */ md5_update(&ctx,sp,sl); /* Then just as many characters of the MD5(pw,salt,pw) */ md5_init(&ctx1); md5_update(&ctx1,pw,strlen(pw)); md5_update(&ctx1,sp,sl); md5_update(&ctx1,pw,strlen(pw)); memcpy(final, md5_final(&ctx1), 16); for(pl = strlen(pw); pl > 0; pl -= 16) md5_update(&ctx,final,pl>16 ? 16 : pl); /* Don't leave anything around in vm they could use. */ memset(final,0,sizeof final); /* Then something really weird... */ for (j=0,i = strlen(pw); i ; i >>= 1) if(i&1) md5_update(&ctx, final+j, 1); else
static int cmd_vfs_md5(struct vmm_chardev *cdev, const char *path) { int fd, rc, i; u32 len; size_t buf_rd; u8 buf[VFS_LOAD_BUF_SZ]; struct stat st; struct md5_context md5c; u8 digest[16]; fd = vfs_open(path, O_RDONLY, 0); if (fd < 0) { vmm_cprintf(cdev, "Failed to open %s\n", path); return fd; } rc = vfs_fstat(fd, &st); if (rc) { vfs_close(fd); vmm_cprintf(cdev, "Failed to stat %s\n", path); return rc; } if (!(st.st_mode & S_IFREG)) { vfs_close(fd); vmm_cprintf(cdev, "Cannot read %s\n", path); return VMM_EINVALID; } len = st.st_size; md5_init(&md5c); while (len) { memset(buf, 0, sizeof(buf)); buf_rd = (len < VFS_LOAD_BUF_SZ) ? len : VFS_LOAD_BUF_SZ; buf_rd = vfs_read(fd, buf, buf_rd); if (buf_rd < 1) { break; } md5_update(&md5c, buf, buf_rd); } md5_final(digest, &md5c); vmm_cprintf(cdev, "MD5 Digest: "); for (i = 0; i < 16; i++) vmm_cprintf(cdev, "%x", digest[i]); vmm_cprintf(cdev, "\n"); rc = vfs_close(fd); if (rc) { vmm_cprintf(cdev, "Failed to close %s\n", path); return rc; } return VMM_OK; }
const char *chirp_ticket_digest(const char *pk) { unsigned char digest[TICKET_DIGEST_LENGTH]; md5_context_t context; md5_init(&context); md5_update(&context, (const unsigned char *) pk, strlen(pk)); md5_final(digest, &context); return md5_string(digest); /* static memory */ }
static char * md5 (const char *input) { md5_init(); // memcpy ((char *) state, (char *) md5_initstate, sizeof (md5_initstate)); // length = 0; md5_update (input, strlen (input)); return (char *)md5_final (); }
void masterconn_sendregister(masterconn *eptr) { uint8_t *buff; uint32_t myip; uint16_t myport; uint64_t usedspace,totalspace; uint64_t tdusedspace,tdtotalspace; uint32_t chunkcount,tdchunkcount; myip = csserv_getlistenip(); myport = csserv_getlistenport(); if (eptr->new_register_mode) { #ifdef MFSDEBUG syslog(LOG_NOTICE,"register ver. 6 - init + space info"); #endif hdd_get_space(&usedspace,&totalspace,&chunkcount,&tdusedspace,&tdtotalspace,&tdchunkcount); if (eptr->gotrndblob && AuthCode) { md5ctx md5c; buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+16+4+4+2+2+2+8+8+4+8+8+4); put8bit(&buff,60); md5_init(&md5c); md5_update(&md5c,eptr->rndblob,16); md5_update(&md5c,(const uint8_t *)AuthCode,strlen(AuthCode)); md5_update(&md5c,eptr->rndblob+16,16); md5_final(buff,&md5c); buff+=16; } else { buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+4+4+2+2+2+8+8+4+8+8+4); put8bit(&buff,60); } put32bit(&buff,VERSHEX); put32bit(&buff,myip); put16bit(&buff,myport); put16bit(&buff,Timeout); put16bit(&buff,masterconn_getcsid()); put64bit(&buff,usedspace); put64bit(&buff,totalspace); put32bit(&buff,chunkcount); put64bit(&buff,tdusedspace); put64bit(&buff,tdtotalspace); put32bit(&buff,tdchunkcount); } else { #ifdef MFSDEBUG syslog(LOG_NOTICE,"register ver. 5 - init"); #endif buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+4+4+2+2); put8bit(&buff,50); put32bit(&buff,VERSHEX); put32bit(&buff,myip); put16bit(&buff,myport); if (Timeout>0) { put16bit(&buff,Timeout); } else { put16bit(&buff,10); } } }
static void md5_sum (uint8_t *dst, const uint8_t *src, const int len) { md5_t ctx[1]; md5_init (ctx); md5_update (ctx, src, len); md5_final (ctx, dst); }
static int md5_close (struct digest_context *cx, u8 *out, int atomic) { md5_ctx_t *ctx = (md5_ctx_t *) cx->digest_info; md5_final (ctx, out); return 0; }
void chirp_ticket_name(const char *root, const char *pk, char *ticket_subject, char *ticket_filename) { unsigned char digest[TICKET_DIGEST_LENGTH]; md5_context_t context; md5_init(&context); md5_update(&context, (const unsigned char *) pk, strlen(pk)); md5_final(digest, &context); sprintf(ticket_subject, TICKET_SUBJECT_FORMAT, md5_string(digest)); sprintf(ticket_filename, "%s/" TICKET_FILENAME_FORMAT, root, md5_string(digest)); }
term_t bif_md5_final1(term_t Context, process_t *ctx) { apr_byte_t *data; if (!is_binary(Context) || int_value2(bin_size(Context)) != sizeof(md5_ctx_t)) return A_BADARG; data = xalloc(proc_gc_pool(ctx), 16); md5_final(data, (md5_ctx_t *)bin_data(Context)); result(make_binary(intnum(MD5_DIGESTSIZE), data, proc_gc_pool(ctx))); return AI_OK; }
const char *md5sum(const char *chaine, size_t len) { struct md5_ctx ctx; unsigned char digest(16); md5_init(&ctx); ctx.size = len?len:strlen(chaine); strcpy(ctx.buf, chaine); md5_update(&ctx); md5_final(digest, &ctx); return digest; }
static ssize_t _sol_message_digest_md5_read_digest(struct sol_message_digest *handle, void *mem, size_t len) { md5_ctx_t *ctx = sol_message_digest_common_get_context(handle); if (len < MD5_DIGEST_LENGTH) return -EINVAL; md5_final(ctx, mem); return len; }
static bool verify_credentials(struct apop_auth_request *request, const unsigned char *credentials, size_t size) { unsigned char digest[16]; struct md5_context ctx; md5_init(&ctx); md5_update(&ctx, request->challenge, strlen(request->challenge)); md5_update(&ctx, credentials, size); md5_final(&ctx, digest); return memcmp(digest, request->response_digest, 16) == 0; }
static void MDFilter (void) { MD5_CONTEXT context; int len; unsigned char buffer [16], digest [16]; md5_init (&context); while (len = fread (buffer, 1, 16, stdin)) md5_update (&context, buffer, len); md5_final (digest, &context); MDPrint (digest); printf ("\n"); }
static void MDString (const char *string) { MD5_CONTEXT context; unsigned char digest [16]; unsigned len = strlen (string); md5_init (&context); md5_update (&context, string, len); md5_final (digest, &context); printf ("MD5 (\"%s\") = ", string); MDPrint (digest); printf ("\n"); }
static void domd5(uint8_t result[MD5_SIZE], FILE *in, size_t size) { struct md5_ctx ctx; md5_init(&ctx); while (size) { char buf[256]; size_t bsize = MIN(size, sizeof(buf)); assert(fread (buf, 1, bsize, in) == bsize); md5_update(&ctx, buf, bsize); size -= bsize; } md5_final(result, &ctx); }
/* * Creates a MD5 digest in hex fomrat (lowercase letters) from the * string S. hextdigest but be buffer of at lease 33 bytes! */ static void md5_hex_digest(char *hexdigest, const unsigned char *s) { int i; MD5_CONTEXT context; unsigned char digest[16]; md5_init(&context); md5_update(&context, s, strlen((gchar *) s)); md5_final(digest, &context); for (i = 0; i < 16; i++) sprintf(hexdigest + 2 * i, "%02x", digest[i]); }
void md5hex_license_ipaddr(geoipupdate_s * gu, const char *client_ipaddr, char *new_digest_str) { unsigned char digest[16]; MD5_CONTEXT context; md5_init(&context); md5_write(&context, (unsigned char *)gu->license.license_key, strlen(gu->license.license_key)); md5_write(&context, (unsigned char *)client_ipaddr, strlen(client_ipaddr)); md5_final(&context); memcpy(digest, context.buf, 16); for (int i = 0; i < 16; i++) { snprintf(&new_digest_str[2 * i], 3, "%02x", digest[i]); } }
void pg_md5_encrypt(const char *part1, const char *part2, size_t part2len, char *dest) { struct md5_ctx ctx; uint8_t hash[MD5_DIGEST_LENGTH]; md5_reset(&ctx); md5_update(&ctx, part1, strlen(part1)); md5_update(&ctx, part2, part2len); md5_final(&ctx, hash); memcpy(dest, "md5", 3); hash2hex(hash, dest + 3); }