Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <time.h>
- #include <stdio.h>
- //Give the function pointer a pretty name
- //Defines the API that users must fit
- typedef void(*RNGFunc)(char*,int);
- void gen_rsa_key(RNGFunc rng) {
- //I want 10 random bytes!
- char buff[10];
- rng(buff, 10);
- //use those random bytes...
- //etc
- }
- //Fill arr with len random bytes
- void my_bad_rng(char *arr, int len) {
- srand(time(NULL));
- for(int i=0; i < len; i++) {
- arr[i] = (char)(rand() % 255);
- }
- }
- void better_unix_rng(char *arr, int len) {
- FILE* urandom = fopen("/dev/urandom", "r");
- if (urandom != NULL) {
- fread(arr, 1, 10, urandom);
- } else {
- //couldn't open /dev/urandom
- //PANIC!!
- abort();
- }
- }
- int main() {
- gen_rsa_key(my_bad_rng);
- gen_rsa_key(better_unix_rng);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement