{{fbdoc item="title" value="cryptlib"}}----
A powerful security toolkit which allows even inexperienced crypto programmers to easily add encryption and authentication services to their software.

Website: http://www.cs.auckland.ac.nz/~pgut001/cryptlib/
Platforms supported: ""Win32"", Linux
Headers to include: cryptlib.bi
Header version: from 2005
Examples: in examples/math/cryptlib/

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/libraries/cryptlib.bas"}}%%(freebasic)
#include once "cryptlib.bi"

function calc_hash( byval filename as string, byval algo as CRYPT_ALGO_TYPE ) as string
	const BUFFER_SIZE = 8192
	dim as byte buffer( 0 to BUFFER_SIZE-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 until( eof( f ) )
		dim as integer oldpos = seek( f )
		get #f, , buffer()
		dim as integer readlength = seek( f ) - oldpos
		'' encrypt
		cryptEncrypt( ctx, @buffer(0), readlength )
	loop

	'' close input file
	close #f

	'' finalize
	cryptEncrypt( ctx, 0, 0 )

	'' get the hash result
	dim as integer buffersize = BUFFER_SIZE
	cryptGetAttributeString( ctx, CRYPT_CTXINFO_HASHVALUE, @buffer(0), @buffersize )

	'' convert to hexadecimal
	dim as string result = ""
	for i as integer = 0 to buffersize-1
		result += hex( buffer(i) )
	next
	
	'' free the context
	cryptDestroyContext( ctx )

	return result
end function

	dim as string filename = trim( command(1) )
	if( len( filename ) = 0 ) then
		print "Usage: hash.exe filename"
		end -1
	end if

	'' init cryptlib
	cryptInit( )

	'' calculate hashes
	print "md5: "; calc_hash( filename, CRYPT_ALGO_MD5 )
	print "sha: "; calc_hash( filename, CRYPT_ALGO_SHA )

	'' shutdown cryptlib
	cryptEnd( )

	sleep
%%

{{fbdoc item="back" value="ExtLibTOC|External Library Table of Contents"}}
   
