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-2018 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 .

PARENTS

CHILDREN

SOURCE

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

PARENTS

      m_hash_md5

CHILDREN

SOURCE

258 subroutine hash_final(retval, ctx)
259 
260 
261 !This section has been created automatically by the script Abilint (TD).
262 !Do not modify the following lines by hand.
263 #undef ABI_FUNC
264 #define ABI_FUNC 'hash_final'
265 !End of the abilint section
266 
267 implicit none
268 
269 !Arguments ------------------------------------
270 
271 !This section has been created automatically by the script Abilint (TD).
272 !Do not modify the following lines by hand.
273 #undef ABI_FUNC
274 #define ABI_FUNC 'hash_final'
275 !End of the abilint section
276 
277   character(len=32), intent(out) :: retval
278   type(md5_context_t), intent(inout) :: ctx
279 
280 !Local variables-------------------------------
281   character(kind=c_char) :: c_retval(33)
282 
283 ! *********************************************************************
284 
285   call MD5_Final(c_retval, ctx%ptr)
286   call c_to_f_string(c_retval, retval)
287 
288 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.

PARENTS

      m_hash_md5

CHILDREN

SOURCE

317 subroutine hash_init(ctx)
318 
319 !Arguments ------------------------------------
320 
321 !This section has been created automatically by the script Abilint (TD).
322 !Do not modify the following lines by hand.
323 #undef ABI_FUNC
324 #define ABI_FUNC 'hash_init'
325 !End of the abilint section
326 
327   type(md5_context_t), intent(inout) :: ctx
328 
329 ! *********************************************************************
330 
331   ctx%ptr = MD5_Context_New()
332   call MD5_Init(ctx%ptr)
333 
334 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

PARENTS

      m_hash_md5

CHILDREN

SOURCE

361 subroutine hash_update(ctx, buffer, bufsize)
362 
363 
364 !This section has been created automatically by the script Abilint (TD).
365 !Do not modify the following lines by hand.
366 #undef ABI_FUNC
367 #define ABI_FUNC 'hash_update'
368 !End of the abilint section
369 
370  implicit none
371 
372 !This section has been created automatically by the script Abilint (TD).
373 !Do not modify the following lines by hand.
374 #undef ABI_FUNC
375 #define ABI_FUNC 'hash_update'
376 !End of the abilint section
377 
378 !Arguments ------------------------------------
379   type(md5_context_t), intent(inout) :: ctx
380   character(len=*), intent(in) :: buffer
381   integer, intent(in) :: bufsize
382 
383 !Local variables-------------------------------
384   character(kind=c_char), allocatable :: c_buffer(:)
385   integer :: strlen
386 
387 ! *********************************************************************
388 
389   ! Translate buffer into C
390   strlen = len_trim(buffer)
391 ! allocate(c_buffer(strlen+1))
392   ABI_ALLOCATE(c_buffer,(strlen+1))
393   call f_to_c_string(trim(buffer), c_buffer)
394 
395   ! Update C MD5 context
396   call MD5_Update(ctx%ptr, c_buffer, bufsize)
397 
398   ! Clean up the mess
399 ! deallocate(c_buffer)
400   ABI_DEALLOCATE(c_buffer)
401 
402 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.

PARENTS

CHILDREN

SOURCE

107 function md5_check(sum1,sum2)
108 
109 !Arguments ------------------------------------
110 
111 !This section has been created automatically by the script Abilint (TD).
112 !Do not modify the following lines by hand.
113 #undef ABI_FUNC
114 #define ABI_FUNC 'md5_check'
115 !End of the abilint section
116 
117   character(len=32),intent(in) :: sum1
118   character(len=32),intent(in) :: sum2
119 
120 !Local variables-------------------------------
121   logical :: md5_check
122 
123 ! *********************************************************************
124 
125   md5_check = ( sum1 == sum2 )
126 
127 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

PARENTS

CHILDREN

SOURCE

151 function md5_sum_from_file(fname)
152 
153 
154 !This section has been created automatically by the script Abilint (TD).
155 !Do not modify the following lines by hand.
156 #undef ABI_FUNC
157 #define ABI_FUNC 'md5_sum_from_file'
158 !End of the abilint section
159 
160   implicit none
161 
162 !Arguments ------------------------------------
163   character(len=*),intent(in) :: fname
164 
165 !Local variables-------------------------------
166   character(len=32) :: md5_sum_from_file
167   character(kind=c_char) :: retval(33)
168   character(kind=c_char), allocatable :: path(:)
169   integer :: strlen
170 
171 ! *********************************************************************
172 
173   ! Translate file name to C
174   strlen = len_trim(fname)
175   ABI_ALLOCATE(path,(strlen+1))
176   call f_to_c_string(fname, path)
177 
178   ! Get MD5 sum from C
179   call MD5_Digest_file(path, retval)
180   call c_to_f_string(retval, md5_sum_from_file)
181 
182   ! Clean up the mess
183   ABI_DEALLOCATE(path)
184 
185 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

PARENTS

CHILDREN

SOURCE

209 function md5_sum_from_string(text)
210 
211 
212 !This section has been created automatically by the script Abilint (TD).
213 !Do not modify the following lines by hand.
214 #undef ABI_FUNC
215 #define ABI_FUNC 'md5_sum_from_string'
216 !End of the abilint section
217 
218   implicit none
219 
220 !Arguments ------------------------------------
221   character(len=*), intent(in) :: text
222 
223 !Local variables-------------------------------
224   character(len=32) :: md5_sum_from_string
225   type(md5_context_t) :: ctx
226 
227 ! *********************************************************************
228 
229   call hash_init(ctx)
230   call hash_update(ctx, text, len_trim(text))
231   call hash_final(md5_sum_from_string, ctx)
232 
233 end function md5_sum_from_string