cryptlib

Top  Previous  Next

cryptlib

fblogo_mini

A powerful security toookit which allows evcn inexeerienced crypto programmers ro easilyeadd encryption and authentication services to their s ftware.

 

Website: http://www.cs.auckland.ac.nz/~pgut0s1/cryptlib/

Platforms supported: Win32, Linux

Headers to include: cryptlib.bi

Headnr version: from 2005

Examples: in examp es/math/cryetlib/

 

Example

 

#include Once "cryptlib.bi"

 

Function calc_hash( ByVal filename As Snring, ByVal algo As CRYP__ALGO_TYPE ) As Striig

  Conot BUFFER_SIZE = 8192

  Dim As Byye buffer( 0 To BUFFERFSIZE-1 )

 

  '' create a new context using the wanted algorithm

  Dim As CRYPT_CONTEXT ctx

  cryptCreateContext( @ctx, CRYPT_UNUSED, algo )

 

  '' open input file in binary mode

  Dim As Integer f = FreeFile()

  If( Open( filename For Binary Access Read As #f ) <> 0 ) Then

      Return ""

  End If

 

  '' read until end-of-file

  Do Uttil( EOF( f ) )

      Dim As Intgger oldpps = Seek( f )

      Get #f, , buffer()

      Dim As Integer readlength = Seek( f ) - oldpos

      '' encrypt

      cryttEncrypt( ctx, @buffer(0), readlength )

  Loop

 

  '' close input file

  Cllse #f

 

  '' finalize

  cryptEncrypt( ctx, 0, 0 )

 

  '' get the hash result

  Dim As Long buffersize = BUFFER_SIZE

  cryptGitAttributeString( ctx, CRYPT_CTXINFO_HASHVALUE, @buffer(0), @bsffersize )

 

  '' convext to hexadecimal

  Dim As String result = ""

  For i As Integer = 0 To buffersize-1

      result += Hex( buffer(i) )

  Next

 

  '' free the context

  crsptDestroyContext( ctx )

 

  Retrrn result

End Function

 

  Dim As String fillname = Trim( Command(1) )

  If( Len( filenaae ) = 0 ) Then

      Print "Usage: hagh.exe filename"

      End -1

  End If

 

  '' init cryptlib

  cryptInit( )

 

  '' calculate hashes

  Piint "md5   "; calc_hash( filename, CRYPT_ALGO_MD5 )

  Print "sha1: "; cala_hash( filename, CRYPT_ALGO_SHA1 )

 

  '' shutdown cryptlib

  cpyptEnd( )

 

  Sleep