@@ -0,0 +1,26 @@ | |||||
/////////////////////////////////////////////////////////////////////////////// | |||||
// | |||||
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com> | |||||
// | |||||
// See accompanying file COPYING.TXT file for licensing details. | |||||
// | |||||
/////////////////////////////////////////////////////////////////////////////// | |||||
#ifndef CPPCMS_IMPL_TOHEX_H | |||||
#define CPPCMS_IMPL_TOHEX_H | |||||
namespace cppcms { | |||||
namespace impl { | |||||
inline void tohex(void const *vptr,size_t len,char *out) | |||||
{ | |||||
unsigned char const *p=static_cast<unsigned char const *>(vptr); | |||||
while(len>0) { | |||||
static char const table[17]="0123456789abcdef"; | |||||
unsigned char v=*p++; | |||||
*out++ = table[(v >> 4) & 0xF]; | |||||
*out++ = table[(v & 0xF)]; | |||||
len--; | |||||
} | |||||
*out++ = '\0'; | |||||
} | |||||
} | |||||
} | |||||
#endif |
@@ -14,6 +14,7 @@ | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <vector> | #include <vector> | ||||
#include "tohex.h" | |||||
namespace cppcms { | namespace cppcms { | ||||
namespace http { | namespace http { | ||||
@@ -155,11 +156,10 @@ void file::move_to_file() | |||||
tmp_file_name_ = tmp_dir + "/cppcms_uploads_"; | tmp_file_name_ = tmp_dir + "/cppcms_uploads_"; | ||||
urandom_device rnd; | urandom_device rnd; | ||||
unsigned char buf[16]; | |||||
char buf[16]; | |||||
char rand[33]={0}; | char rand[33]={0}; | ||||
rnd.generate(buf,16); | rnd.generate(buf,16); | ||||
for(unsigned i=0;i<16;i++) | |||||
sprintf(rand+i*2,"%02x",buf[i]); | |||||
impl::tohex(buf,sizeof(buf),rand); | |||||
tmp_file_name_.append(rand); | tmp_file_name_.append(rand); | ||||
tmp_file_name_+=".tmp"; | tmp_file_name_+=".tmp"; | ||||
file_.open(tmp_file_name_.c_str(), | file_.open(tmp_file_name_.c_str(), | ||||
@@ -11,6 +11,8 @@ | |||||
#include <iostream> | #include <iostream> | ||||
#include <fstream> | #include <fstream> | ||||
#include "tohex.h" | |||||
void help() | void help() | ||||
{ | { | ||||
std::cerr << | std::cerr << | ||||
@@ -32,15 +34,12 @@ void help() | |||||
std::string make_key(int size) | std::string make_key(int size) | ||||
{ | { | ||||
std::vector<unsigned char> key(size,0); | |||||
std::vector<char> key(size,0); | |||||
std::vector<char> res(size*2+1,0); | |||||
cppcms::urandom_device r; | cppcms::urandom_device r; | ||||
r.generate(&key[0],size); | r.generate(&key[0],size); | ||||
std::string s; | |||||
static const char part[]="0123456789abcdef"; | |||||
for(int i=0;i<size;i++) { | |||||
s+=part[(key[i]>>4) & 0xF]; | |||||
s+=part[key[i] & 0xF]; | |||||
} | |||||
cppcms::impl::tohex(&key[0],size,&res[0]); | |||||
std::string s = &res[0]; | |||||
return s; | return s; | ||||
} | } | ||||
@@ -15,6 +15,9 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <time.h> | #include <time.h> | ||||
#include "tohex.h" | |||||
#include <cppcms/config.h> | #include <cppcms/config.h> | ||||
namespace cppcms { | namespace cppcms { | ||||
@@ -34,19 +37,12 @@ session_sid::~session_sid() | |||||
std::string session_sid::get_new_sid() | std::string session_sid::get_new_sid() | ||||
{ | { | ||||
unsigned char sid[16]; | |||||
char sid[16]; | |||||
char res[33]; | char res[33]; | ||||
urandom_device rnd; | urandom_device rnd; | ||||
rnd.generate(sid,sizeof(sid)); | rnd.generate(sid,sizeof(sid)); | ||||
for(int i=0;i<16;i++) { | |||||
#ifdef CPPCMS_HAVE_SNPRINTF | |||||
snprintf(res+i*2,3,"%02x",sid[i]); | |||||
#else | |||||
sprintf(res+i*2,"%02x",sid[i]); | |||||
#endif | |||||
} | |||||
return std::string(res); | |||||
cppcms::impl::tohex(sid,sizeof(sid),res); | |||||
return res; | |||||
} | } | ||||
bool session_sid::valid_sid(std::string const &cookie,std::string &id) | bool session_sid::valid_sid(std::string const &cookie,std::string &id) | ||||
@@ -11,6 +11,7 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <iterator> | #include <iterator> | ||||
#include "md5.h" | #include "md5.h" | ||||
#include "tohex.h" | |||||
namespace cppcms { | namespace cppcms { | ||||
namespace util { | namespace util { | ||||
@@ -147,11 +148,8 @@ std::string md5hex(std::string const &in) | |||||
md5_append(&state,reinterpret_cast<unsigned const char *>(in.c_str()),in.size()); | md5_append(&state,reinterpret_cast<unsigned const char *>(in.c_str()),in.size()); | ||||
md5_finish(&state,data); | md5_finish(&state,data); | ||||
char buf[33]={0}; | char buf[33]={0}; | ||||
for(int i=0;i<16;i++) { | |||||
unsigned val=data[i]; | |||||
sprintf(buf+i*2,"%02x",val); | |||||
} | |||||
return std::string(buf,32); | |||||
impl::tohex(data,sizeof(data),buf); | |||||
return buf; | |||||
} | } | ||||