void md5_hash (md5_t* self, uint32_t hash[4]) { size_t i = self->bufsize; self->buffer[i++] = 0x80; // append single bit to message while (i < 64) self->buffer[i++] = 0x00; // pad with zeros if (self->bufsize >= 55) { md5_digest(self); for (i = 0; i < 64; ++i) self->buffer[i] = 0x00; } self->bitsize += self->bufsize * 8; self->buffer[56] = self->bitsize; self->buffer[57] = self->bitsize >> 8; self->buffer[58] = self->bitsize >> 16; self->buffer[59] = self->bitsize >> 24; self->buffer[60] = self->bitsize >> 32; self->buffer[61] = self->bitsize >> 40; self->buffer[62] = self->bitsize >> 48; self->buffer[63] = self->bitsize >> 56; md5_digest(self); hash[0] = SWAP(self->rawhash[0]); hash[1] = SWAP(self->rawhash[1]); hash[2] = SWAP(self->rawhash[2]); hash[3] = SWAP(self->rawhash[3]); }
DECLARE_TEST(md5, reference) { md5_t* md5; char md5str[32]; string_t digest; md5 = md5_allocate(); md5_digest_finalize(md5); md5_digest(md5, "testing md5 implementation", 26); md5_digest_finalize(md5); digest = md5_get_digest(md5, md5str, sizeof(md5str)); EXPECT_STRINGEQ(digest, string_const(STRING_CONST("4E24E37E5E06F23210FA1518E97A50C4"))); md5_digest(md5, "testing md5 implementation", 26); md5_digest(md5, "", 0); md5_digest(md5, "further testing md5 implementation with long buffer > 32 bytes", 62); md5_digest_finalize(md5); digest = md5_get_digest(md5, md5str, sizeof(md5str)); EXPECT_STRINGEQ(digest, string_const(STRING_CONST("BD870884942EA7B32A9CB2547B02B871"))); md5_digest(md5, digest_test_string, 2000); md5_digest_finalize(md5); digest = md5_get_digest(md5, md5str, sizeof(md5str)); EXPECT_STRINGEQ(digest, string_const(STRING_CONST("137D3C94230A0E230C4DDFC97EACCCD2"))); md5_deallocate(md5); return 0; }
static void pbkdf1_md5(const char *password, unsigned password_len, const uint8_t salt[8], unsigned iter_count, unsigned key_size, uint8_t *key) { struct md5_ctx ctx; uint8_t tmp[16]; unsigned i; if (key_size > sizeof(tmp)) abort(); for (i=0;i<iter_count;i++) { md5_init(&ctx); if (i==0) { md5_update(&ctx, password_len, (uint8_t*)password); md5_update(&ctx, 8, salt); md5_digest(&ctx, 16, tmp); } else { md5_update(&ctx, 16, tmp); md5_digest(&ctx, 16, tmp); } } memcpy(key, tmp, key_size); return; }
RSSBonusInfo *rss_register(Boards *boards, md5_byte_t mimd5[16], char *link, board_msg_info *mi) { RSSBonusInfo *ri; int i; ALLOC_OBJ(ri, RSSBonusInfo); ri->link = NULL; ri->id = mi->id; if (link && strlen(link)) { ri->link = strdup(link); } else { ri->link = strdup("(none)"); } md5_digest(ri->link,ri->linkmd5); //printf("reghister = '%s', md5 = %02x%02x..%02x\n", link, ri->linkmd5[0], ri->linkmd5[1], ri->linkmd5[15]); memcpy(ri->md5,mimd5,16); if (boards->nb_rss_e == boards->max_rss_e) { boards->max_rss_e *= 2; boards->rss_e = realloc(boards->rss_e, sizeof(RSSBonusInfo*)*boards->max_rss_e); assert(boards->rss_e); } boards->rss_e[boards->nb_rss_e++] = ri; for (i = boards->nb_rss_e - 1; i; --i) { if (memcmp(boards->rss_e[i-1]->linkmd5, boards->rss_e[i]->linkmd5, 16) > 0) { RSSBonusInfo *tmp = boards->rss_e[i]; boards->rss_e[i] = boards->rss_e[i-1]; boards->rss_e[i-1] = tmp; } else break; } /*for (i=0; i < boards->nb_rss_e; ++i) { printf(" rss %d/%d : %02x%02x..%02x\n", i, boards->nb_rss_e, boards->rss_e[i]->linkmd5[0], boards->rss_e[i]->linkmd5[1], boards->rss_e[i]->linkmd5[15]); }*/ return ri; }
int pkcs1_rsa_md5_encode(mpz_t m, size_t key_size, struct md5_ctx *hash) { uint8_t *p; TMP_GMP_DECL(em, uint8_t); TMP_GMP_ALLOC(em, key_size); p = _pkcs1_signature_prefix(key_size, em, sizeof(md5_prefix), md5_prefix, MD5_DIGEST_SIZE); if (p) { md5_digest(hash, MD5_DIGEST_SIZE, p); nettle_mpz_set_str_256_u(m, key_size, em); TMP_GMP_FREE(em); return 1; } else { TMP_GMP_FREE(em); return 0; } }
int main(void) { unsigned char string[256]; struct md5_ctx ctx; unsigned int size; int ret; #ifdef REPEAT do { #else print_string("This is the emulated program. Will try to calculate MD5...\n"); #endif md5_init(&ctx); /* pass the string address above */ #ifdef TEST #define STR "A long text to hash, and hash, and hash and hash..." size = sizeof(STR)-1; memcpy(string, STR, size); md5_update(&ctx, size, string); md5_digest(&ctx, 16, string); return 0; #else ret = SYSCALL2(USER_SYSCALL(1), (unsigned int)string, sizeof(string)); if (ret > 0) { size = ret; md5_update(&ctx, size, string); md5_digest(&ctx, 16, string); #ifndef REPEAT ret = SYSCALL2(USER_SYSCALL(2), (unsigned int)string, 16); if (ret == 0) return 0; #endif } #endif #ifdef REPEAT } while(SYSCALL1(USER_SYSCALL(0),0)==0); #endif return 1; }
void SmugMug::WebService::_uploadHttpPut ( const QString &AlbumId, const QString &FileName, const QString &Caption) { QFileInfo fi (FileName); _dataOut.clear (); QFile file (fi.absoluteFilePath ()); if (file.open (QIODevice::ReadOnly)) { _dataOut = file.readAll (); file.close (); } #if 0 QUrl url (UploadEndpoint + "photos/xmlrawadd.mg"); QHttpRequestHeader header("POST", url.path ()); #else QUrl url (UploadEndpoint + fi.fileName ()); QHttpRequestHeader header("PUT", url.path ()); #endif header.setValue("Host", url.host ()); header.setContentLength (_dataOut.length ()); // header.setContentType ("image/jpeg"); // header.setValue("content-length", QString::number (_dataOut.length ())); header.setValue("Content-MD5", md5_digest (_dataOut)); // header.setValue("Content-MD5", "a234ab01efe2775e9f69477831c3d3ca"); // header.setValue("Content-Transfer-Encoding", "binary"); // header.setValue("Keep-Alive", "100"); // header.setValue("Connection", "keep-alive"); header.setValue("X-Smug-SessionID", _sessionId); header.setValue("X-Smug-Version", "1.2.0"); header.setValue("X-Smug-ResponseType", "REST"); header.setValue("X-Smug-AlbumID", AlbumId); header.setValue("X-Smug-FileName", fi.fileName ()); if (!Caption.isEmpty ()) { // header.setValue("X-Smug-Caption", Caption); } qDebug () << "----- http request header --------------------------" << endl << header.toString (); _idImageUpload = _httpRequest (url, header, _dataOut); qDebug () << "imageUpload: " << url.toString (); }
void md5_update (md5_t* self, byte_t* data, size_t length) { for (size_t i = 0; i < length; ++i) { self->buffer[self->bufsize] = data[i]; self->bufsize++; if (self->bufsize == 64) { md5_digest(self); self->bitsize += 512; self->bufsize = 0; } } }
void Curl_gtls_md5sum(unsigned char *tmp, /* input */ size_t tmplen, unsigned char *md5sum, /* output */ size_t md5len) { #if defined(USE_GNUTLS_NETTLE) struct md5_ctx MD5pw; md5_init(&MD5pw); md5_update(&MD5pw, tmplen, tmp); md5_digest(&MD5pw, md5len, md5sum); #elif defined(USE_GNUTLS) gcry_md_hd_t MD5pw; gcry_md_open(&MD5pw, GCRY_MD_MD5, 0); gcry_md_write(MD5pw, tmp, tmplen); memcpy(md5sum, gcry_md_read (MD5pw, 0), md5len); gcry_md_close(MD5pw); #endif }
RSSBonusInfo * rss_find_from_link(Boards *boards, char *link) { int i0, i1; md5_byte_t md5[16]; md5_digest(link,md5); //printf("link = '%s', md5 = %02x%02x..%02x\n", link, md5[0], md5[1], md5[15]); i0 = 0; i1 = boards->nb_rss_e-1; while (i1>=i0) { int i = (i0 + i1)/2; //printf("i0=%d, i1=%d, compare with %02x%02x..%02x\n", i0, i1, boards->rss_e[i]->linkmd5[0], boards->rss_e[i]->linkmd5[1], boards->rss_e[i]->linkmd5[15]); int cmp = memcmp(boards->rss_e[i]->linkmd5, md5, 16); if (i1 <= i0 && cmp) return NULL; if (cmp < 0) { i0 = i+1; } else if (cmp > 0) { i1 = i-1; } else return boards->rss_e[i]; } return NULL; }
char *userdb_mkmd5pw(const char *buf) { int i; char salt[9]; salt[8]=0; #ifdef RANDOM userdb_get_random(salt, 8); for (i=0; i<8; i++) salt[i] = userdb_hex64[salt[i] & 63 ]; #else { struct { #if HAVE_GETTIMEOFDAY struct timeval tv; #else time_t tv; #endif pid_t p; } s; MD5_DIGEST d; #if HAVE_GETTIMEOFDAY struct timezone tz; gettimeofday(&s.tv, &tz); #else time(&s.tv); #endif s.p=getpid(); md5_digest(&s, sizeof(s), d); for (i=0; i<8; i++) salt[i]=userdb_hex64[ ((unsigned char *)d)[i] ]; } #endif return (md5_crypt(buf, salt)); }
string drm_digest(string mixed) { std::string output; //std::string mixed = ""; //const char* pBuffer = _pEnv->GetStringUTFChars(drm_version, NULL); //if (pBuffer != NULL) //{ // mixed += pBuffer; // _pEnv->ReleaseStringUTFChars(drm_version, pBuffer); //} //pBuffer = _pEnv->GetStringUTFChars(deviceId, NULL); //if (pBuffer != NULL) //{ // mixed += pBuffer; // _pEnv->ReleaseStringUTFChars(deviceId, pBuffer); //} //pBuffer = _pEnv->GetStringUTFChars(drm_client_key, NULL); //if (pBuffer != NULL) //{ // mixed += pBuffer; // _pEnv->ReleaseStringUTFChars(drm_client_key, pBuffer); //} // md5 MD5_DIGEST digest; md5_digest( mixed.c_str(), (int)mixed.size(), digest ); // base64 char *encoded; int len = base64_encode( digest, sizeof( digest ), &encoded ); output = std::string( encoded ); free( encoded ); return output; }
int md5_sum(FILE *input, char output_string[BASE16_ENCODE_LENGTH(MD5_DIGEST_SIZE)]) { unsigned char bytes[BUFFER_SIZE]; size_t bytes_read; struct md5_ctx md5; uint8_t digest[MD5_DIGEST_SIZE]; md5_init(&md5); while((bytes_read = fread(bytes, sizeof(unsigned char), BUFFER_SIZE, input)) > 0) { md5_update(&md5, bytes_read, bytes); } if(ferror(input) != 0) { fprintf(stderr, "Error reading input file: %s.\n", strerror(errno)); return -1; } md5_digest(&md5, MD5_DIGEST_SIZE, digest); // And build the hex checksum the nettle way ;) base16_encode_update((uint8_t *)output_string, MD5_DIGEST_SIZE, digest); return 0; }
int main() { static const char * const teststr[]={ "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890"}; char *salts[4]={"abcdef","01234567","76543210","QWERTY"}; char *passwds[4]={ "rosebud", "trust noone", "trust, but verify", "for the world is hollow, and I have touched the sky"}; int i,j; printf("MD5 test suite:\n"); for (i=0; i<(int)sizeof(teststr)/sizeof(teststr[0]); i++) { MD5_DIGEST digest; md5_digest(teststr[i], strlen(teststr[i]), digest); printf("MD5 (\"%s\") = ", teststr[i]); for (j=0; j<sizeof(digest); j++) printf("%02x", digest[j]); printf("\n"); } for (i=0; i<sizeof(salts)/sizeof(salts[0]); i++) printf("Salt: %s\nPassword: %s\nHash:%s\n\n", salts[i], passwds[i], md5_crypt_redhat(passwds[i], salts[i])); return (0); }
/** * Compute 128bit MD5 digest for string. * * @param string from which digest shall be computed * @return Allocated C string containing the hex result representation is * returned. It should be passed to free(3). On failure NULL is returned. */ char * md5(const char *string) { return md5_digest(1, string); }
static void MD5_Final(unsigned char digest[16], MD5_CTX *ctx) { md5_digest(ctx, 16, digest); }
uint128_t stream_md5(stream_t* stream) { size_t cur, ic, lastc, num, limit; md5_t md5; uint128_t ret = uint128_null(); unsigned char buf[1025]; bool ignore_lf = false; if (stream->vtable->md5) return stream->vtable->md5(stream); if (stream_is_sequential(stream) || !(stream->mode & STREAM_IN)) return ret; cur = stream_tell(stream); stream_seek(stream, 0, STREAM_SEEK_BEGIN); md5_initialize(&md5); limit = sizeof(buf)-1; buf[limit] = 0; while (!stream_eos(stream)) { num = stream->vtable->read(stream, buf, limit); if (!num) continue; if (stream->mode & STREAM_BINARY) md5_digest(&md5, buf, (size_t)num); else { //If last buffer ended with CR, ignore a leading LF lastc = 0; if (ignore_lf && (buf[0] == '\n')) lastc = 1; ignore_lf = false; //Digest one line at a time //Treat all line endings (LF, CR, CR+LF) as Unix style LF. If file has mixed line endings //(for example, one line ending in a single CR and next is empty and ending in a single LF), //it will not work! /*lint -e{850} */ for (ic = lastc; ic < num && ic < limit; ++ic) { bool was_cr = (buf[ic] == '\r'); bool was_lf = (buf[ic] == '\n'); if (was_cr || was_lf) { if (was_cr && (ic == limit-1)) ignore_lf = true; //Make next buffer ignore leading LF as it is part of CR+LF buf[ic] = '\n'; md5_digest(&md5, buf + lastc, (size_t)((ic - lastc) + 1)); //Include the LF if (was_cr && (buf[ic + 1] == '\n')) //Check for CR+LF ++ic; lastc = ic + 1; } } if (lastc < num) md5_digest(&md5, buf + lastc, (size_t)(num - lastc)); } } stream_seek(stream, (ssize_t)cur, STREAM_SEEK_BEGIN); md5_digest_finalize(&md5); ret = md5_get_digest_raw(&md5); md5_finalize(&md5); return ret; }
void SmugMug::WebService::_uploadMultipartHttpPost ( const QString &AlbumId, const QString &FileName, const QString &Caption) { const QString BOUNDARY = "----------ThIs_Is_tHe_bouNdaRY_$"; const QString CFLF = "\r\n"; QUrl url (UploadEndpoint + "/photos/xmladd.mg"); _dataOut.clear (); QByteArray imageData; QFileInfo fi (FileName); QFile file (fi.absoluteFilePath ()); if (file.open (QIODevice::ReadOnly)) { imageData = file.readAll (); file.close (); } // Album ID _dataOut += QString ("--") + BOUNDARY + CFLF; _dataOut += QString ("Content-Disposition: form-data; name=\"AlbumID\"") + CFLF + CFLF; _dataOut += AlbumId + CFLF; // Session ID _dataOut += QString ("--") + BOUNDARY + CFLF; _dataOut += QString ("Content-Disposition: form-data; name=\"SessionID\"") + CFLF + CFLF; _dataOut += _sessionId + CFLF; // Byte Count _dataOut += QString ("--") + BOUNDARY + CFLF; _dataOut += QString ("Content-Disposition: form-data; name=\"ByteCount\"") + CFLF + CFLF; _dataOut += QString::number (imageData.length ()) + CFLF; // Response Type _dataOut += QString ("--") + BOUNDARY + CFLF; _dataOut += QString ("Content-Disposition: form-data; name=\"ResponseType\"") + CFLF + CFLF; _dataOut += QString ("REST") + CFLF; // MD5 Sum _dataOut += QString ("--") + BOUNDARY + CFLF; _dataOut += QString ("Content-Disposition: form-data; name=\"MD5Sum\"") + CFLF + CFLF; _dataOut += md5_digest (imageData) + CFLF; // _dataOut += QString ("a234ab01efe2775e9f69477831c3d3ca") + CFLF; // Caption if (!Caption.isEmpty ()) { _dataOut += QString ("--") + BOUNDARY + CFLF; _dataOut += QString ("Content-Disposition: form-data; name=\"Caption\"") + CFLF + CFLF; _dataOut += Caption + CFLF; } // Add the file _dataOut += QString ("--") + BOUNDARY + CFLF; _dataOut += QString ("Content-Disposition: form-data; name=\"Image\"") + QString ("; filename=\"%1\"").arg (fi.fileName ()) + CFLF; _dataOut += QString ("Content-Type: image/jpeg") + CFLF; _dataOut += QString ("Content-length: %1").arg (imageData.length ()) + CFLF + CFLF; qDebug () << _dataOut; _dataOut += imageData; _dataOut += CFLF; _dataOut += QString ("--") + BOUNDARY + "--" + CFLF; QHttpRequestHeader header ("POST", url.path ()); header.setValue ("Host", url.host ()); // header.setValue ("Cookie", QString ("SMSESS=") + _sessionId); header.setContentType (QString ("multipart/form-data; boundary=") + BOUNDARY); header.setContentLength (_dataOut.length ()); // header.setValue ("Connection", "Keep-Alive"); qDebug () << header.toString (); _idImageUpload = _httpRequest (url, header, _dataOut); qDebug () << "imageUpload:" << fi.absoluteFilePath (); }
static void test(gpointer data, gpointer user_data) { int tn = *((int *)data), dn = *((int *)user_data); printf("===> test start: %d\n", tn); val_t vals[dn]; int i, mul=1; while (mul < dn) mul *= 10; for (i=0; i<dn; i++) { vals[i].num = tn*mul + i; vals[i].tm = time(NULL); md5_digest(&vals[i].num, sizeof(int), vals[i].key.digest); DelData(cache, &vals[i].key); } for (i=0; i<dn; i++) { sleep(time(NULL)%2); int result; size_t data_size=sizeof(val_t); result = AddData(cache, &vals[i].key, (void *)&vals[i], data_size); if (result != 0) { ret = 1; val_t *v = &vals[i]; printf("+>set error: v(%p).num = %d\n", v, ((val_t *)v)->num); } } for (i=0; i<dn; i++) { val_t *v; size_t data_size; GetData(cache, &vals[i].key, (void *)&v, &data_size); if (v == NULL) { ret = 2; printf("-> get wrong: v=%p\n", v); } else { if (v != &vals[i]) { ret = 2; printf("-> get wrong: v(%p) != vals[%d](%p)\n", v, i, &vals[i]); } if (v->num != vals[i].num) { ret = 2; printf("-> get wrong: v->num(%d) != vals[%d].num(%d)\n", v->num, i, vals[i].num); } if (v->tm != vals[i].tm) { ret = 2; printf("-> get wrong: v->tm (%d) != vals[%d].tm (%d)\n", v->tm, i, vals[i].tm); } if (md5_compare(((map_key_t)v)->digest, ((map_key_t)&vals[i].key)->digest)!=0) { ret = 2; printf("-> get wrong: md5\n"); } } } for (i=0; i<dn; i++) { int result; result = DelData(cache, &vals[i].key); if (result != 0) { ret = 3; printf("-> remove wrong: v=%p\n", &vals[i]); } } printf("===> test stop : %d\n", tn); run--; }
static int __archive_nettle_md5final(archive_md5_ctx *ctx, void *md) { md5_digest(ctx, MD5_DIGEST_SIZE, md); return (ARCHIVE_OK); }
int main(void) { u8 h[sizeof client_hello_bin_hash]; md5_digest(client_hello_bin, sizeof client_hello_bin, h); return memcmp(h, client_hello_bin_hash, sizeof h); }