Commit 84c3d114 authored by Matthieu Dien's avatar Matthieu Dien
Browse files

Agg bignum from str and md5

parent 92b11924
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>

typedef mpz_t* bignum_t;
@@ -15,6 +16,13 @@ bignum_t bignum_from_int(int n){
  return x;
}

bignum_t bignum_from_str(char* s, int base){
  bignum_t x = (bignum_t) malloc(sizeof(mpz_t));
  mpz_set_str(*x, s, base);
  return x;
}


bignum_t bignum_copy(bignum_t x){
  bignum_t y = bignum_zero();
  mpz_set(*y, *x);
@@ -97,6 +105,18 @@ bignum_t bignum_gcd(bignum_t a, bignum_t b){
  return g;
}

bignum_t bignum_from_MD5(unsigned char* md5sum, size_t len){
  char s[100];
  size_t r = 0;
  bignum_t res = bignum_zero();

  for(size_t i = 0; i < len; i++)
    r = r + sprintf(s+r, "%x", md5sum[i]);

  mpz_set_str(*res, s, 16);
  return res;
}

char* bignum_to_str(bignum_t x){
  return mpz_get_str(NULL, 10, *x);
}
+3 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ typedef mpz_t* bignum_t;

bignum_t bignum_zero();
bignum_t bignum_from_int(int);
bignum_t bignum_from_str(char*, int);
bignum_t bignum_copy(bignum_t);
void bignum_destroy(bignum_t);

@@ -26,5 +27,7 @@ bignum_t bignum_pow(bignum_t, bignum_t);

bignum_t bignum_gcd(bignum_t, bignum_t);

bignum_t bignum_from_MD5(unsigned char* md5sum, size_t len);

char* bignum_to_str(bignum_t);
#endif
+17 −1
Original line number Diff line number Diff line
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <openssl/md5.h>
#include "bignum.h"

#define BG(n) bignum_from_int(n)
@@ -35,11 +36,26 @@ void test_str(){
  printf("test_str OK\n");
}

void test_from_md5(){
  bignum_t t, tt;
  unsigned char digest[MD5_DIGEST_LENGTH];
  char x[] = "coucou";
  char xx[] = "719d8179d0cea7774b518d58fd52fc4";
  MD5((const unsigned char*)x, 7, digest);
  t = bignum_from_MD5(digest, MD5_DIGEST_LENGTH);
  tt = bignum_from_str(xx, 16);
  
  assert(bignum_eq(t, tt));
  
  printf("test_from_md5 OK\n");
}

int main(){
  printf("=== BIGNUM tests ===\n");
  test_arithmetic();
  test_cmp();
  test_str();
  test_from_md5();
  
  return 0;
}