How to resolve the algorithm MD5 step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm MD5 step by step in the PARI/GP programming language

Table of Contents

Problem Statement

Encode a string using an MD5 algorithm.   The algorithm can be found on   Wikipedia.

Optionally, validate your implementation by running all of the test values in   IETF RFC (1321)   for MD5. Additionally,   RFC 1321   provides more precise information on the algorithm than the Wikipedia article. If the solution on this page is a library solution, see   MD5/Implementation   for an implementation from scratch.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm MD5 step by step in the PARI/GP programming language

Source code in the pari/gp programming language

#include 
#include 

#define HEX(x)  (((x) < 10)? (x)+'0': (x)-10+'a')

/*
 * PARI/GP func: MD5 hash
 *
 * gp code: install("plug_md5", "s", "MD5", "");
 */
GEN plug_md5(char *text)
{
  char md[MD5_DIGEST_LENGTH];
  char hash[sizeof(md) * 2 + 1];
  int i;

  MD5((unsigned char*)text, strlen(text), (unsigned char*)md);

  for (i = 0; i < sizeof(md); i++) {
    hash[i+i]   = HEX((md[i] >> 4) & 0x0f);
    hash[i+i+1] = HEX(md[i] & 0x0f);
  }

  hash[sizeof(md) * 2] = 0;

  return strtoGENstr(hash);
}


install("plug_md5", "s", "MD5", "~/libmd5.so");

MD5("The quick brown fox jumped over the lazy dog's back")

md5( message ) = { my( 
   a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476,
   s = [ 7, 12, 17, 22; 5, 9, 14, 20; 4, 11, 16, 23; 6, 10, 15, 21 ],
   msg = concat( Vec( concat(Vecsmall(message), Vecsmall(128)), (#message + 9) \ 64 * 64 + 56 ),
     Vecrev(digits( #message % 2^61 * 8, 256), 8)), \\ little endian !
   leftrotate(x, c) = x << c + x >> (32-c)
  );
  \\***  Process the message in successive 512-bit chunks ***
  forstep( m=1, #msg, 512/8, 
    my( \\ break chunk into 32-bit words M[j] - LITTLE ENDIAN !
       M = vector( 16, j, fromdigits( Vecrev(msg[ m + j*4 - 4 .. m + j*4 - 1 ]), 2^8 )),
       A = a, B = b, C = c, D = d); 
    for( i = 0 , 63 , [A, D, C, B] = [D, C, B, B + leftrotate( ( A + abs(sin(i+1))\2^-32 +
       if( i < 16 , bitor( bitand(B , C) , bitand( bitneg(B) , D)) + M[ i +1]
         , i < 32 , bitor( bitand(D , B) , bitand( bitneg(D) , C)) + M[ (5*i + 1) % 16 +1]
         , i < 48 , bitxor( bitxor( B , C ) , D ) + M[ (3*i + 5) % 16 +1]
         , bitxor( C , bitor( bitneg(D), B)) + M[ 7*i % 16 +1]
       )) % 2^32, s[i\16+1, i%4+1] )]
    ); \\ end for
    [a,b,c,d] = ([a,b,c,d] + [A,B,C,D]) % 2^32 ;
  ); \\ end forstep
  Strprintf("%032x",fromdigits(Vecrev(digits(fromdigits([d,c,b,a],2^32),256),16),256))
}

gp > md5("")

gp > md5("The quick brown fox jumps over lazy dogs.")

  

You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Tcl programming language
You may also check:How to resolve the algorithm Comments step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Teacup rim text step by step in the Wren programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the 11l programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the Kotlin programming language