2011年4月21日 星期四

Cryptographic Foibles

Using Poor Random Numbers

直接看 Chromium project 怎麼解:

In Windows platform,
uint32 RandUint32() {
uint32 number;
CHECK_EQ(rand_s(&number), 0);
return number;
}

In POSIX platform,
class URandomFd {
public:
URandomFd() {
fd_ = open("/dev/urandom", O_RDONLY);
CHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno;
}

~URandomFd() {
close(fd_);
}

int fd() const { return fd_; }

private:
int fd_;
};

base::LazyInstance g_urandom_fd(base::LINKER_INITIALIZED);

uint64 RandUint64() {
uint64 number;

int urandom_fd = g_urandom_fd.Pointer()->fd();
bool success = file_util::ReadFromFD(
urandom_fd,
reinterpret_cast(&number),
sizeof(number));
CHECK(success);

return number;
}



Creating Your Own Cryptographic Functions

Do not, under any circumstances, create your own encryption algorithm.

沒有留言:

張貼留言