[Gambas-user] Does gb.crypt only handle passwords?

Manu mtitouinfo at yahoo.fr
Tue May 3 17:29:55 CEST 2022


hello

with gb.openssl

dim algo As String[] = Digest.List, ans As String
Print "\nOpenssl Cipher password"
Print "algo: ", algo.Join(", ")
ans = Cipher["AES-128-CBC"].EncryptSalted("hello", "MySecret")
Print "EncryptSalted", Base64(ans)
ans = Cipher["AES-128-CBC"].DecryptSalted(ans, "MySecret")
Print "DecryptSalted", ans

-->
Openssl Cipher password
algo:   DSA, DSA-SHA, ecdsa-with-SHA1, MD4, MD5, RIPEMD160, SHA, SHA1, 
SHA224, SHA256, SHA384, SHA512, whirlpool
EncryptSalted   U2FsdGVkX1+CyF22HcBnVvbkK9en0JqfE5puJSlllX0=
DecryptSalted   hello

------------------------------------------------------------------
------------------------------------------------------------------
Private Function Str2Hex(target As String) As String
   Dim res As String = ""
   For i As Integer = 1 To Len(target)
     res &= Hex$(Asc(target, i), 2)
   Next
   Return LCase(res)
End

Private Function String2Hex(target As String, Optional format As String) 
As String
'test other method
Dim hStream As Stream, tmp As Byte, res As String = ""
hStream = Open String target For Read
   While Not Eof(hStream)
     tmp = Read #hStream As Byte
     'Print tmp;;
     'Print LCase(Hex(tmp, 2));;
     Select Case format
      Case Like "{hex*,x}"
       res &= LCase(Hex(tmp, 2)) & " "
       Case "csv"
       res &= LCase(Hex(tmp, 2)) & ","
      Case Else
       res &= LCase(Hex(tmp, 2))
     End Select
   Wend
Return res
End

Private Function md5sum(tgt As String) As String
Dim md As String = "", res As String = ""
'System.Shell = "/bin/bash"
If Access(File.RealPath(tgt), gb.Read)
   'tgt is file
   Shell "md5sum " & tgt Wait To md
   res = "file= "
Else
   ' tgt is simple string
   Shell "md5sum <<<" & tgt Wait To md
   res = "string= "
Endif
Try md = Split(md, " ")[0]
Return res & md
End
Private Function md5samba(target As String) As String
''md5sum native gambas
Dim res, tmp As String = ""
   If Access(target, gb.Read)
   'target is a file
     target = File.RealPath(target)
     tmp = File.Load(target)
     tmp = Str2Hex(Digest["md5"].Hash(tmp))
     res = "file= " & tmp
   Else
   'target is a simple string
   target = target & "\n"
   tmp = Str2Hex(Digest["md5"].Hash(target))
   res = "string= " & tmp
   Endif
Return res
End

Private Sub Hexdamp(target As String, Optional format As String)
   '' it simulates hexdump -C if target is a file,  returns String2Hex 
if target is a string
Dim res As String = "", tmp As String = "", hFile As File, sBuffer As 
String, rest As Byte, c As Byte
   If Access(File.RealPath(target), gb.Read)
     'target is a file
     target = File.RealPath(target)

     hFile = Open target For Read
     rest = Lof(hFile) - Int(Lof(hFile) / 16) * 16
     'Print "lof", Lof(hFile)
     'Print "turns", Lof(hFile) / 16
     'Print "rest", rest

     For i As Byte = 1 To (Lof(hFile) - rest) Step 16
     sBuffer = Read #hFile, 16
     tmp = String2Hex(sBuffer, format)
     Print tmp & "-->" & sBuffer
     Next

     If rest > 0 Then
         sBuffer = Read #hFile, rest
         tmp = String2Hex(sBuffer, format)
         Print tmp & "-->" & sBuffer
     Endif
    Close #hFile
    Print "file = " & target

   Else
     'target is a simple string
     target = target & "\n"
     tmp = String2Hex(target, format)
     Print "string=" & Quote(target) & "\t" & tmp
   Endif
End

Public Sub Examples()
Dim h As String, algo As String[] = Digest.List, ans As String
Dim hFile As File, sBuffer As String, hStream As Stream
System.Shell = "/bin/bash"  'must if it exists "<<<"  (Function md5sum)

Print "md5sum compares file.load vs open read "
hFile = Open "...boat.jpeg" For Read
Read #hFile, sBuffer, Lof(hFile)
h = File.Load("...boat.jpeg")
h = String2Hex(Digest["md5"].Hash(h))
Print "Method file.load, md5sum", h
h = String2Hex(Digest["md5"].Hash(sBuffer))
Print "Method Open Read, md5sum", h

Print "\nOpenssl Cipher password"
Print "algo: ", algo.Join(", ")
ans = Cipher["AES-128-CBC"].EncryptSalted("hello", "MySecret")
Print "EncryptSalted", Base64(ans)
ans = Cipher["AES-128-CBC"].DecryptSalted(ans, "MySecret")
Print "DecryptSalted", ans

Print "\nopenssl hash\nmd5sum string"
Print "hello"
Print "a", md5sum("hello")
Print "b", Str2Hex(Digest["md5"].Hash("hello\n"))
Print "c", String2Hex(Digest["md5"].Hash("hello\n"))

Print "\nConvert StringToHexadecimal"
Print "Le Père Nöel (2020)!", String2Hex("Le Père Nöel (2020)!")
Print "Le Père Nöel (2020)!", Str2Hex("Le Père Nöel (2020)!")

Print "\nmd5sum File"
Print "boat", md5sum("...boat.jpeg")

Print "\nmd5samba"
Print md5samba("hello")
Print md5samba("...boat.jpeg")

Print "\nhexdamp(hexdump -C)\n"
Hexdamp("...hello.txt", "")
Hexdamp("...hello.txt", "csv")
Hexdamp("...hello.txt", "x")
Hexdamp("Hello !", "")
End
----
you can use "echo -n" instead of "<<<"
----
Goodbye



More information about the User mailing list