TABLE OF CONTENTS


ABINIT/m_hash_md5 [ Modules ]

[ Top ] [ Modules ]

NAME

  m_hash_md5

FUNCTION

  This module provides resources to calculate MD5 checksums.

COPYRIGHT

  Copyright (C) 2016-2022 ABINIT group (Yann Pouillon)
  This file is distributed under the terms of the
  GNU General Public License, see ~abinit/COPYING
  or http://www.gnu.org/copyleft/gpl.txt .

SOURCE

16 #if defined HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include "abi_common.h"
21 
22 module m_hash_md5
23 
24   use, intrinsic :: iso_c_binding
25   use m_abicore
26 
27   implicit none
28 
29   private
30 
31   public :: md5_check             ! Checks whether two MD5 sums are identical
32   public :: md5_sum_from_file     ! Computes a MD5 sum from a file
33   public :: md5_sum_from_string   ! Computes a MD5 sum from a text string
34 
35   type :: md5_context_t
36     private
37     type(c_ptr) :: ptr = C_NULL_PTR
38   end type md5_context_t
39 
40   interface
41     function MD5_Context_New() bind(c, name="MD5_Context_New")
42       import
43       type(c_ptr) :: MD5_Context_New
44     end function MD5_Context_New
45   end interface
46   interface
47     subroutine MD5_Digest_File(fname, retval) bind(c, name="MD5_Digest_File")
48       import
49       character(kind=c_char) :: fname(*)
50       character(kind=c_char) :: retval(33)
51     end subroutine MD5_Digest_File
52   end interface
53   interface
54     subroutine MD5_Final(retval, ctx) bind(c, name="MD5_Final")
55       import
56       character(kind=c_char) :: retval(33)
57       type(c_ptr), value :: ctx
58     end subroutine MD5_Final
59   end interface
60   interface
61     subroutine MD5_Init(ctx) bind(c, name="MD5_Init")
62       import
63       type(c_ptr), value :: ctx
64     end subroutine MD5_Init
65   end interface
66   interface
67     subroutine MD5_Update(ctx, buffer, bufsize) bind(c, name="MD5_Update")
68       import
69       type(c_ptr), value :: ctx
70       character(kind=c_char) :: buffer(*)
71       integer(c_int), value :: bufsize
72     end subroutine MD5_Update
73   end interface
74 
75 contains  !===========================================================

m_hash_md5/hash_final [ Functions ]

[ Top ] [ m_hash_md5 ] [ Functions ]

NAME

  hash_final

FUNCTION

  Builds the final return value of the MD5 checksum.

INPUTS

  ctx = MD5 context object

OUTPUT

  retval = string containing the MD5 checksum

SOURCE

211 subroutine hash_final(retval, ctx)
212 
213 !Arguments ------------------------------------
214   character(len=32), intent(out) :: retval
215   type(md5_context_t), intent(inout) :: ctx
216 
217 !Local variables-------------------------------
218   character(kind=c_char) :: c_retval(33)
219 
220 ! *********************************************************************
221 
222   call MD5_Final(c_retval, ctx%ptr)
223   call c_to_f_string(c_retval, retval)
224 
225 end subroutine hash_final

m_hash_md5/hash_init [ Functions ]

[ Top ] [ m_hash_md5 ] [ Functions ]

NAME

 hash_init

FUNCTION

  Checks whether two MD5 sums are identical.

INPUTS

  ctx = MD5 context object

SIDE EFFECTS

  ctx is reset to its intial values

NOTES

  Created a function to be able to add more operations than just checking
  the equality of the sums.

SOURCE

249 subroutine hash_init(ctx)
250 
251 !Arguments ------------------------------------
252   type(md5_context_t), intent(inout) :: ctx
253 
254 ! *********************************************************************
255 
256   ctx%ptr = MD5_Context_New()
257   call MD5_Init(ctx%ptr)
258 
259 end subroutine hash_init

m_hash_md5/hash_update [ Functions ]

[ Top ] [ m_hash_md5 ] [ Functions ]

NAME

 hash_update

FUNCTION

  Updates a MD5 context object.

INPUTS

  ctx = MD5 context object
  buffer = data to process
  bufsize = number of bytes to process

SIDE EFFECTS

  ctx gets updated with the new data

SOURCE

281 subroutine hash_update(ctx, buffer, bufsize)
282 
283 !Arguments ------------------------------------
284   type(md5_context_t), intent(inout) :: ctx
285   character(len=*), intent(in) :: buffer
286   integer, intent(in) :: bufsize
287 
288 !Local variables-------------------------------
289   character(kind=c_char), allocatable :: c_buffer(:)
290   integer :: strlen
291 
292 ! *********************************************************************
293 
294   ! Translate buffer into C
295   strlen = len_trim(buffer)
296 ! allocate(c_buffer(strlen+1))
297   ABI_MALLOC(c_buffer,(strlen+1))
298   call f_to_c_string(trim(buffer), c_buffer)
299 
300   ! Update C MD5 context
301   call MD5_Update(ctx%ptr, c_buffer, bufsize)
302 
303   ! Clean up the mess
304 ! deallocate(c_buffer)
305   ABI_FREE(c_buffer)
306 
307 end subroutine hash_update

m_hash_md5/md5_check [ Functions ]

[ Top ] [ m_hash_md5 ] [ Functions ]

NAME

 md5_check

FUNCTION

  Checks whether two MD5 sums are identical.

INPUTS

  sum1 = the first sum to compare
  sum2 = the second sum to compare

OUTPUT

  boolean telling whether the two sums are identical

NOTES

  Created a function to be able to add more operations than just checking
  the equality of the sums.

SOURCE

 98 function md5_check(sum1,sum2)
 99 
100 !Arguments ------------------------------------
101   character(len=32),intent(in) :: sum1
102   character(len=32),intent(in) :: sum2
103 
104 !Local variables-------------------------------
105   logical :: md5_check
106 
107 ! *********************************************************************
108 
109   md5_check = ( sum1 == sum2 )
110 
111 end function md5_check

m_hash_md5/md5_sum_from_file [ Functions ]

[ Top ] [ m_hash_md5 ] [ Functions ]

NAME

 md5_sum_from_file

FUNCTION

  Computes a MD5 sum from a file.

INPUTS

  fname = path to the file

OUTPUT

  String representing the MD5 sum of the file

SOURCE

131 function md5_sum_from_file(fname)
132 
133 !Arguments ------------------------------------
134   character(len=*),intent(in) :: fname
135 
136 !Local variables-------------------------------
137   character(len=32) :: md5_sum_from_file
138   character(kind=c_char) :: retval(33)
139   character(kind=c_char), allocatable :: path(:)
140   integer :: strlen
141 
142 ! *********************************************************************
143 
144   ! Translate file name to C
145   strlen = len_trim(fname)
146   ABI_MALLOC(path,(strlen+1))
147   call f_to_c_string(fname, path)
148 
149   ! Get MD5 sum from C
150   call MD5_Digest_file(path, retval)
151   call c_to_f_string(retval, md5_sum_from_file)
152 
153   ! Clean up the mess
154   ABI_FREE(path)
155 
156 end function md5_sum_from_file

m_hash_md5/md5_sum_from_string [ Functions ]

[ Top ] [ m_hash_md5 ] [ Functions ]

NAME

 md5_sum_from_string

FUNCTION

  Computes a MD5 sum from a string.

INPUTS

  text = string to process

OUTPUT

  String representing the MD5 sum of the argument

SOURCE

176 function md5_sum_from_string(text)
177 
178 !Arguments ------------------------------------
179   character(len=*), intent(in) :: text
180 
181 !Local variables-------------------------------
182   character(len=32) :: md5_sum_from_string
183   type(md5_context_t) :: ctx
184 
185 ! *********************************************************************
186 
187   call hash_init(ctx)
188   call hash_update(ctx, text, len_trim(text))
189   call hash_final(md5_sum_from_string, ctx)
190 
191 end function md5_sum_from_string