static void SetAvatar(HANDLE hContact, JABBER_LIST_ITEM *item, char *data, int len, DWORD format) { FILE* out; char filename[MAX_PATH]; char md5[33]; MD5 context; int i; md5_init(&context); md5_update(&context, data, len); md5_finalize(&context); if (format == PA_FORMAT_UNKNOWN && len > 4) { format = JabberGetPictureType(data); } for (i=0;i<16;i++) { char lo, hi; unsigned int j=context.state[i>>2]; j>>=8*(i%4); j&=0xFF; lo = j & 0x0F; hi = j >> 4; hi = hi + ((hi > 9) ? 'a' - 10 : '0'); lo = lo + ((lo > 9) ? 'a' - 10 : '0'); md5[i*2] = hi; md5[i*2+1] = lo; } md5[i*2] = 0; if (item != NULL) { char *hash = item->avatarHash; item->avatarFormat = format; item->avatarHash = mir_strdup(md5); mir_free(hash); } else { jabberThreadInfo->avatarFormat = format; strcpy(jabberThreadInfo->avatarHash, md5); } TlenGetAvatarFileName(item, filename, sizeof filename ); DeleteFileA(filename); out = fopen( filename, "wb" ); if ( out != NULL ) { fwrite( data, len, 1, out ); fclose( out ); DBWriteContactSettingString(hContact, "ContactPhoto", "File", filename ); DBWriteContactSettingString(hContact, jabberProtoName, "AvatarHash", md5); DBWriteContactSettingDword(hContact, jabberProtoName, "AvatarFormat", format); } ProtoBroadcastAck( jabberProtoName, hContact, ACKTYPE_AVATAR, ACKRESULT_STATUS, NULL , 0); }
uint128_t stream_md5( stream_t* stream ) { int64_t cur, ic, offset, lastc, num; md5_t* md5; uint128_t ret = {0}; unsigned char buf[1025]; bool ignore_lf = false; FOUNDATION_ASSERT( stream ); if( stream->vtable->md5 ) return stream->vtable->md5( stream ); if( !stream || stream_is_sequential( stream ) || !( stream->mode & STREAM_IN ) ) return ret; FOUNDATION_ASSERT( stream->vtable->read ); cur = stream_tell( stream ); stream_seek( stream, 0, STREAM_SEEK_BEGIN ); md5 = md5_allocate(); while( !stream_eos( stream ) ) { num = (int64_t)stream->vtable->read( stream, buf, 1024 ); if( !num ) continue; if( stream->mode & STREAM_BINARY ) md5_digest_raw( md5, buf, (size_t)num ); else { //If last buffer ended with CR, ignore a leading LF offset = 0; lastc = 0; if( ignore_lf && ( buf[0] == '\n' ) ) offset = 1; ignore_lf = false; //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 ending in a single LF), it will not work! for( ic = 0; ic < num; ++ic ) { bool was_cr = ( buf[ic] == '\r' ); bool was_lf = ( buf[ic] == '\n' ); if( was_cr || was_lf ) { if( was_cr && ( ic >= 1023 ) ) ignore_lf = true; buf[ic] = '\n'; md5_digest_raw( 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_raw( md5, buf + lastc, (size_t)( num - lastc ) ); } } stream_seek( stream, cur, STREAM_SEEK_BEGIN ); md5_finalize( md5 ); ret = md5_get_digest_raw( md5 ); md5_deallocate( md5 ); return ret; }
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; }
/*! * @param [out] digest хэш md5 * @param [in] string строка * @param [in] len длина строки */ void md5sum(char* digest, const char* string, size_t len) { md5_context_t context; md5_init(&context); md5_update(&context, (const unsigned char*) string, len); md5_finalize(&context, (unsigned char*) digest); }
void oauth_signandappend_oauth_header(const char *reqMethod, struct url_props *url, const char *consumerKey, const char *consumerSecret, const char *authToken, const char *authTokenSecret, const time_t now, const char *postContent, const size_t postContentLen, const char *oauthCallback, const char *oauthVerifier, const char *oauthScope, struct string *out) { struct signctx senv; char _tb[256]; const size_t _tbLen = sprintf(_tb, "%u", (unsigned)now); struct timeval tv; struct md5_context md5Ctx; signctx_init(&senv); init_signature_seed(&senv, reqMethod, url); if (postContentLen) append_signature_params(&senv, postContent, postContent + postContentLen); string_append(out, "Authorization: OAuth ", 21); append_signature_param(&senv, "oauth_consumer_key", 18, consumerKey, strlen(consumerKey)); string_appendfmt(out, "oauth_consumer_key=\"%s\"", consumerKey); append_signature_param(&senv, "oauth_signature_method", 22, "HMAC-SHA1", 9); string_append(out, ",oauth_signature_method=\"HMAC-SHA1\"", 35); string_appendfmt(out,",oauth_timestamp=\"%u\"", (uint32_t)now); append_signature_param(&senv, "oauth_timestamp", 15, _tb, _tbLen); gettimeofday(&tv, NULL); uint8_t digest[16]; char digestAlpha[33]; md5_init(&md5Ctx); md5_update(&md5Ctx, (uint8_t *)&tv.tv_sec, sizeof(tv.tv_sec)); md5_update(&md5Ctx, (uint8_t *)&tv.tv_usec, sizeof(tv.tv_usec)); md5_update(&md5Ctx, (uint8_t *)consumerKey, strlen(consumerKey)); md5_update(&md5Ctx, (uint8_t *)"io.language", 11); md5_finalize(&md5Ctx, digest); md5_string((char *)digest, digestAlpha); const size_t digestAlphaLen = strlen(digestAlpha); // 32 string_appendfmt(out, ",oauth_nonce=\"%.*s\"", digestAlphaLen, digestAlpha); append_signature_param(&senv, "oauth_nonce", 11, digestAlpha, digestAlphaLen); string_appendfmt(out, ",oauth_version=\"1.0\"", 20); append_signature_param(&senv, "oauth_version", 13, "1.0", 3); if (authToken && *authToken != '\0') { string_appendfmt(out, ",oauth_token=\"%s\"", authToken); append_signature_param(&senv, "oauth_token", 11, authToken, strlen(authToken)); } if (oauthCallback && *oauthCallback != '\0') { string_appendfmt(out, ",oauth_callback=\"%s\"", oauthCallback); append_signature_param(&senv, "oauth_callback", 14, oauthCallback, strlen(oauthCallback)); } if (oauthVerifier && *oauthVerifier != '\0') { string_appendfmt(out, ",oauth_verifier=\"%s\"", oauthVerifier); append_signature_param(&senv, "oauth_verifier", 14, oauthVerifier, strlen(oauthVerifier)); } if (oauthScope && *oauthScope != '\0') { string_appendfmt(out, ",scope=\"%s\"", oauthScope); append_signature_param(&senv, "scope", 5, oauthScope, strlen(oauthScope)); } build_signature(&senv, "HMAC-SHA1", consumerSecret, authTokenSecret ? authTokenSecret : ""); string_append(out, ",oauth_signature=\"", 18); string_append_urlencoded_rfc3986(out, string_data(&senv.signatureParamsBuf), string_len(&senv.signatureParamsBuf)); string_append(out, "\"\r\n", 3); signctx_dealloc(&senv); }