elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Librería para calcular hashes y hmacs
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Librería para calcular hashes y hmacs  (Leído 2,452 veces)
Carloswaldo
Traductor
Moderador Global
***
Desconectado Desconectado

Mensajes: 4.764


Nos reservamos el derecho de ban.


Ver Perfil WWW
Librería para calcular hashes y hmacs
« en: 24 Junio 2009, 01:58 am »

Pues que estoy buscando una librería que calcule hashes y hmacs, la necesito para una aplicación, he encontrado la Librería QuickHash, pero es shareware, necesito algo gratis. Si alguien me ayuda se los agradecería. :)


En línea





Dominio en venta: https://forojapones.com/
BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: Librería para calcular hashes y hmacs
« Respuesta #1 en: 24 Junio 2009, 22:42 pm »

Hash par Archivos:

En un Modulo:
Código
  1. '---------------------------------------------------------------------------------------
  2. ' Module      : mFileHash
  3. ' DateTime    : 21/05/2008 06:01
  4. ' Author      : Cobein
  5. ' Mail        : cobein27@hotmail.com
  6. ' WebPage     : http://www.advancevb.com.ar
  7. ' Purpose     : API file hash
  8. ' Usage       : At your own risk
  9. ' Requirements: None
  10. ' Distribution: You can freely use this code in your own
  11. '               applications, but you may not reproduce
  12. '               or publish this code on any web site,
  13. '               online service, or distribute as source
  14. '               on any media without express permission.
  15. '
  16. ' Reference   : http://www.mvps.org/emorcillo/en/code/vb6/index.shtml
  17. '
  18. ' History     : 21/05/2008 First Cut....................................................
  19. '---------------------------------------------------------------------------------------
  20. Option Explicit
  21.  
  22. Private Const BLOCK_SIZE            As Long = 32 * 1024& ' 32K
  23.  
  24. Private Const FILE_SHARE_READ       As Long = &H1
  25. Private Const FILE_SHARE_WRITE      As Long = &H2
  26. Private Const GENERIC_READ          As Long = &H80000000
  27. Private Const INVALID_HANDLE_VALUE  As Long = (-1)
  28. Private Const OPEN_EXISTING         As Long = 3
  29.  
  30. Private Const PROV_RSA_FULL         As Long = 1
  31. Private Const ALG_CLASS_HASH        As Long = 32768
  32. Private Const ALG_TYPE_ANY          As Long = 0
  33. Private Const CRYPT_VERIFYCONTEXT   As Long = &HF0000000
  34.  
  35. Private Const ALG_SID_MD2           As Long = 1
  36. Private Const ALG_SID_MD4           As Long = 2
  37. Private Const ALG_SID_MD5           As Long = 3
  38. Private Const ALG_SID_SHA1          As Long = 4
  39.  
  40. Private Const HP_HASHVAL            As Long = 2
  41. Private Const HP_HASHSIZE           As Long = 4
  42.  
  43. Public Enum HashAlgorithm
  44.    MD2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2
  45.    MD4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4
  46.    md5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
  47.    SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1
  48. End Enum
  49.  
  50. Private Type tFileChunks
  51.    bvChunck()                      As Byte
  52.    lChuncks                        As Long
  53.    bvReminder()                    As Byte
  54.    lReminder                       As Long
  55.    lCount                          As Long
  56. End Type
  57.  
  58. Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
  59. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  60. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  61. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
  62. Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
  63. Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
  64. Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwFlags As Long) As Long
  65. Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, ByRef phHash As Long) As Long
  66. Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal hHash As Long) As Long
  67. Private Declare Function CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, pbData As Byte, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
  68. Private Declare Function CryptGetHashParam Lib "advapi32.dll" (ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, pdwDataLen As Long, ByVal dwFlags As Long) As Long
  69.  
  70. Public Function HashFile(ByVal sFile As String, ByVal eHash As HashAlgorithm, ByRef sHash As String) As Long
  71.    Dim lhFile      As Long
  72.    Dim lFileSize   As Long
  73.    Dim lRet        As Long
  74.    Dim lhContext   As Long
  75.    Dim lhHash      As Long
  76.    Dim tFile       As tFileChunks
  77.    Dim lSize       As Long
  78.  
  79.    If Not PathFileExists(sFile) = 0 Then
  80.  
  81.        lhFile = CreateFile(sFile, _
  82.           GENERIC_READ, _
  83.           FILE_SHARE_READ Or FILE_SHARE_WRITE, _
  84.           ByVal 0&, OPEN_EXISTING, 0, 0)
  85.  
  86.        If Not lhFile = INVALID_HANDLE_VALUE Then
  87.  
  88.            lFileSize = GetFileSize(lhFile, 0&)
  89.  
  90.            If Not lFileSize = 0 Then
  91.  
  92.                lRet = CryptAcquireContext(lhContext, _
  93.                   vbNullString, vbNullString, _
  94.                   PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)
  95.  
  96.                If Not lRet = 0 Then
  97.  
  98.                    lRet = CryptCreateHash(lhContext, _
  99.                       eHash, 0, 0, lhHash)
  100.  
  101.                    If Not lRet = 0 Then
  102.  
  103.                        With tFile
  104.                            ReDim .bvChunck(1 To BLOCK_SIZE)
  105.                            .lChuncks = lFileSize \ BLOCK_SIZE
  106.                            .lReminder = lFileSize - .lChuncks * BLOCK_SIZE
  107.                            If Not .lReminder = 0 Then
  108.                                ReDim .bvReminder(1 To .lReminder)
  109.                            End If
  110.  
  111.                            For .lCount = 1 To .lChuncks
  112.                                Call ReadFile(lhFile, .bvChunck(1), BLOCK_SIZE, 0&, 0&)
  113.                                If CryptHashData(lhHash, .bvChunck(1), BLOCK_SIZE, 0) = 0 Then
  114.                                    Exit For
  115.                                End If
  116.                            Next
  117.  
  118.                            If Not .lReminder = 0 Then
  119.                                Call ReadFile(lhFile, .bvReminder(1), .lReminder, 0&, 0&)
  120.                                lRet = CryptHashData(lhHash, .bvReminder(1), .lReminder, 0)
  121.                            End If
  122.  
  123.                            lRet = CryptGetHashParam(lhHash, HP_HASHSIZE, lSize, 4, 0)
  124.                            If Not lRet = 0 Then
  125.                                ReDim .bvReminder(0 To lSize - 1)
  126.                                lRet = CryptGetHashParam(lhHash, HP_HASHVAL, .bvReminder(0), lSize, 0)
  127.                                If Not lRet = 0 Then
  128.                                    .lCount = 0
  129.                                    For .lCount = 0 To UBound(.bvReminder)
  130.                                        sHash = sHash & Right$("0" & Hex$(.bvReminder(.lCount)), 2)
  131.                                    Next
  132.                                Else
  133.                                    HashFile = 7
  134.                                End If
  135.                            Else
  136.                                HashFile = 6
  137.                            End If
  138.                        End With
  139.                    Else
  140.                        HashFile = 5
  141.                    End If
  142.                Else
  143.                    HashFile = 4
  144.                End If
  145.            Else
  146.                HashFile = 3
  147.            End If
  148.        Else
  149.            HashFile = 2
  150.        End If
  151.    Else
  152.        HashFile = 1
  153.    End If
  154.  
  155.    Call CryptDestroyHash(lhHash)
  156.    Call CryptReleaseContext(lhContext, 0)
  157.    Call CloseHandle(lhFile)
  158. End Function
  159.  

Creditos: Cobein.¡1

Hash para Texto (MD5)

En un Modulo:
Código
  1. Option Explicit
  2.  
  3. Private lngTrack As Long
  4. Private arrLongConversion(4) As Long
  5. Private arrSplit64(63) As Byte
  6.  
  7. Private Const OFFSET_4 = 4294967296#
  8. Private Const MAXINT_4 = 2147483647
  9.  
  10.  
  11.  
  12. Private Const S11 = 7
  13. Private Const S12 = 12
  14. Private Const S13 = 17
  15. Private Const S14 = 22
  16. Private Const S21 = 5
  17. Private Const S22 = 9
  18. Private Const S23 = 14
  19. Private Const S24 = 20
  20. Private Const S31 = 4
  21. Private Const S32 = 11
  22. Private Const S33 = 16
  23. Private Const S34 = 23
  24. Private Const S41 = 6
  25. Private Const S42 = 10
  26. Private Const S43 = 15
  27. Private Const S44 = 21
  28.  
  29. Private Function MD5Round(strRound As String, a As Long, _
  30.  b As Long, C As Long, d As Long, X As Long, S As Long, _
  31.  ac As Long) As Long
  32.  
  33.    Select Case strRound
  34.  
  35.        Case Is = "FF"
  36.            a = MD5LongAdd4(a, (b And C) Or (Not (b) And d), X, ac)
  37.            a = MD5Rotate(a, S)
  38.            a = MD5LongAdd(a, b)
  39.  
  40.        Case Is = "GG"
  41.            a = MD5LongAdd4(a, (b And d) Or (C And Not (d)), X, ac)
  42.            a = MD5Rotate(a, S)
  43.            a = MD5LongAdd(a, b)
  44.  
  45.        Case Is = "HH"
  46.            a = MD5LongAdd4(a, b Xor C Xor d, X, ac)
  47.            a = MD5Rotate(a, S)
  48.            a = MD5LongAdd(a, b)
  49.  
  50.        Case Is = "II"
  51.            a = MD5LongAdd4(a, C Xor (b Or Not (d)), X, ac)
  52.            a = MD5Rotate(a, S)
  53.            a = MD5LongAdd(a, b)
  54.  
  55.    End Select
  56. End Function
  57.  
  58. Private Function MD5Rotate(lngValue As Long, lngBits As Long) As Long
  59.    Dim lngSign As Long
  60.    Dim lngI As Long
  61.  
  62.    lngBits = (lngBits Mod 32)
  63.  
  64.    If lngBits = 0 Then MD5Rotate = lngValue: Exit Function
  65.  
  66.    For lngI = 1 To lngBits
  67.        lngSign = lngValue And &HC0000000
  68.        lngValue = (lngValue And &H3FFFFFFF) * 2
  69.        lngValue = lngValue Or ((lngSign < 0) And 1) Or _
  70.            (CBool(lngSign And &H40000000) And &H80000000)
  71.    Next
  72.  
  73.    MD5Rotate = lngValue
  74. End Function
  75.  
  76. Private Function TRID() As String
  77.    Dim sngNum As Single, lngnum As Long
  78.    Dim strResult As String
  79.  
  80.    sngNum = Rnd(2147483648#)
  81.    strResult = CStr(sngNum)
  82.  
  83.    strResult = Replace(strResult, "0.", "")
  84.    strResult = Replace(strResult, ".", "")
  85.    strResult = Replace(strResult, "E-", "")
  86.  
  87.    TRID = strResult
  88. End Function
  89.  
  90. Private Function MD564Split(lngLength As Long, bytBuffer() As Byte) As String
  91.    Dim lngBytesTotal As Long, lngBytesToAdd As Long
  92.    Dim intLoop As Integer, intLoop2 As Integer, lngTrace As Long
  93.    Dim intInnerLoop As Integer, intLoop3 As Integer
  94.  
  95.    lngBytesTotal = lngTrack Mod 64
  96.    lngBytesToAdd = 64 - lngBytesTotal
  97.    lngTrack = (lngTrack + lngLength)
  98.  
  99.    If lngLength >= lngBytesToAdd Then
  100.        For intLoop = 0 To lngBytesToAdd - 1
  101.            arrSplit64(lngBytesTotal + intLoop) = bytBuffer(intLoop)
  102.        Next intLoop
  103.  
  104.        MD5Conversion arrSplit64
  105.  
  106.        lngTrace = (lngLength) Mod 64
  107.  
  108.        For intLoop2 = lngBytesToAdd To lngLength - intLoop - lngTrace Step 64
  109.            For intInnerLoop = 0 To 63
  110.                arrSplit64(intInnerLoop) = bytBuffer(intLoop2 + intInnerLoop)
  111.            Next intInnerLoop
  112.  
  113.            MD5Conversion arrSplit64
  114.  
  115.        Next intLoop2
  116.  
  117.        lngBytesTotal = 0
  118.    Else
  119.  
  120.      intLoop2 = 0
  121.  
  122.    End If
  123.  
  124.    For intLoop3 = 0 To lngLength - intLoop2 - 1
  125.  
  126.        arrSplit64(lngBytesTotal + intLoop3) = bytBuffer(intLoop2 + intLoop3)
  127.  
  128.    Next intLoop3
  129. End Function
  130.  
  131. Private Function MD5StringArray(strInput As String) As Byte()
  132.    Dim intLoop As Integer
  133.    Dim bytBuffer() As Byte
  134.    ReDim bytBuffer(Len(strInput))
  135.  
  136.    For intLoop = 0 To Len(strInput) - 1
  137.        bytBuffer(intLoop) = Asc(Mid(strInput, intLoop + 1, 1))
  138.    Next intLoop
  139.  
  140.    MD5StringArray = bytBuffer
  141. End Function
  142.  
  143. Private Sub MD5Conversion(bytBuffer() As Byte)
  144.    Dim X(16) As Long, a As Long
  145.    Dim b As Long, C As Long
  146.    Dim d As Long
  147.  
  148.    a = arrLongConversion(1)
  149.    b = arrLongConversion(2)
  150.    C = arrLongConversion(3)
  151.    d = arrLongConversion(4)
  152.  
  153.    MD5Decode 64, X, bytBuffer
  154.  
  155.    MD5Round "FF", a, b, C, d, X(0), S11, -680876936
  156.    MD5Round "FF", d, a, b, C, X(1), S12, -389564586
  157.    MD5Round "FF", C, d, a, b, X(2), S13, 606105819
  158.    MD5Round "FF", b, C, d, a, X(3), S14, -1044525330
  159.    MD5Round "FF", a, b, C, d, X(4), S11, -176418897
  160.    MD5Round "FF", d, a, b, C, X(5), S12, 1200080426
  161.    MD5Round "FF", C, d, a, b, X(6), S13, -1473231341
  162.    MD5Round "FF", b, C, d, a, X(7), S14, -45705983
  163.    MD5Round "FF", a, b, C, d, X(8), S11, 1770035416
  164.    MD5Round "FF", d, a, b, C, X(9), S12, -1958414417
  165.    MD5Round "FF", C, d, a, b, X(10), S13, -42063
  166.    MD5Round "FF", b, C, d, a, X(11), S14, -1990404162
  167.    MD5Round "FF", a, b, C, d, X(12), S11, 1804603682
  168.    MD5Round "FF", d, a, b, C, X(13), S12, -40341101
  169.    MD5Round "FF", C, d, a, b, X(14), S13, -1502002290
  170.    MD5Round "FF", b, C, d, a, X(15), S14, 1236535329
  171.  
  172.    MD5Round "GG", a, b, C, d, X(1), S21, -165796510
  173.    MD5Round "GG", d, a, b, C, X(6), S22, -1069501632
  174.    MD5Round "GG", C, d, a, b, X(11), S23, 643717713
  175.    MD5Round "GG", b, C, d, a, X(0), S24, -373897302
  176.    MD5Round "GG", a, b, C, d, X(5), S21, -701558691
  177.    MD5Round "GG", d, a, b, C, X(10), S22, 38016083
  178.    MD5Round "GG", C, d, a, b, X(15), S23, -660478335
  179.    MD5Round "GG", b, C, d, a, X(4), S24, -405537848
  180.    MD5Round "GG", a, b, C, d, X(9), S21, 568446438
  181.    MD5Round "GG", d, a, b, C, X(14), S22, -1019803690
  182.    MD5Round "GG", C, d, a, b, X(3), S23, -187363961
  183.    MD5Round "GG", b, C, d, a, X(8), S24, 1163531501
  184.    MD5Round "GG", a, b, C, d, X(13), S21, -1444681467
  185.    MD5Round "GG", d, a, b, C, X(2), S22, -51403784
  186.    MD5Round "GG", C, d, a, b, X(7), S23, 1735328473
  187.    MD5Round "GG", b, C, d, a, X(12), S24, -1926607734
  188.  
  189.    MD5Round "HH", a, b, C, d, X(5), S31, -378558
  190.    MD5Round "HH", d, a, b, C, X(8), S32, -2022574463
  191.    MD5Round "HH", C, d, a, b, X(11), S33, 1839030562
  192.    MD5Round "HH", b, C, d, a, X(14), S34, -35309556
  193.    MD5Round "HH", a, b, C, d, X(1), S31, -1530992060
  194.    MD5Round "HH", d, a, b, C, X(4), S32, 1272893353
  195.    MD5Round "HH", C, d, a, b, X(7), S33, -155497632
  196.    MD5Round "HH", b, C, d, a, X(10), S34, -1094730640
  197.    MD5Round "HH", a, b, C, d, X(13), S31, 681279174
  198.    MD5Round "HH", d, a, b, C, X(0), S32, -358537222
  199.    MD5Round "HH", C, d, a, b, X(3), S33, -722521979
  200.    MD5Round "HH", b, C, d, a, X(6), S34, 76029189
  201.    MD5Round "HH", a, b, C, d, X(9), S31, -640364487
  202.    MD5Round "HH", d, a, b, C, X(12), S32, -421815835
  203.    MD5Round "HH", C, d, a, b, X(15), S33, 530742520
  204.    MD5Round "HH", b, C, d, a, X(2), S34, -995338651
  205.  
  206.    MD5Round "II", a, b, C, d, X(0), S41, -198630844
  207.    MD5Round "II", d, a, b, C, X(7), S42, 1126891415
  208.    MD5Round "II", C, d, a, b, X(14), S43, -1416354905
  209.    MD5Round "II", b, C, d, a, X(5), S44, -57434055
  210.    MD5Round "II", a, b, C, d, X(12), S41, 1700485571
  211.    MD5Round "II", d, a, b, C, X(3), S42, -1894986606
  212.    MD5Round "II", C, d, a, b, X(10), S43, -1051523
  213.    MD5Round "II", b, C, d, a, X(1), S44, -2054922799
  214.    MD5Round "II", a, b, C, d, X(8), S41, 1873313359
  215.    MD5Round "II", d, a, b, C, X(15), S42, -30611744
  216.    MD5Round "II", C, d, a, b, X(6), S43, -1560198380
  217.    MD5Round "II", b, C, d, a, X(13), S44, 1309151649
  218.    MD5Round "II", a, b, C, d, X(4), S41, -145523070
  219.    MD5Round "II", d, a, b, C, X(11), S42, -1120210379
  220.    MD5Round "II", C, d, a, b, X(2), S43, 718787259
  221.    MD5Round "II", b, C, d, a, X(9), S44, -343485551
  222.  
  223.    arrLongConversion(1) = MD5LongAdd(arrLongConversion(1), a)
  224.    arrLongConversion(2) = MD5LongAdd(arrLongConversion(2), b)
  225.    arrLongConversion(3) = MD5LongAdd(arrLongConversion(3), C)
  226.    arrLongConversion(4) = MD5LongAdd(arrLongConversion(4), d)
  227. End Sub
  228.  
  229. Private Function MD5LongAdd(lngVal1 As Long, lngVal2 As Long) As Long
  230.    Dim lngHighWord As Long
  231.    Dim lngLowWord As Long
  232.    Dim lngOverflow As Long
  233.  
  234.    lngLowWord = (lngVal1 And &HFFFF&) + (lngVal2 And &HFFFF&)
  235.  
  236.    lngOverflow = lngLowWord \ 65536
  237.  
  238.    lngHighWord = (((lngVal1 And &HFFFF0000) \ 65536) _
  239.        + ((lngVal2 And &HFFFF0000) \ 65536) _
  240.        + lngOverflow) And &HFFFF&
  241.  
  242.    MD5LongAdd = MD5LongConversion((lngHighWord * 65536#) _
  243.        + (lngLowWord And &HFFFF&))
  244. End Function
  245.  
  246. Private Function MD5LongAdd4(lngVal1 As Long, _
  247.  lngVal2 As Long, lngVal3 As Long, lngVal4 As Long) As Long
  248.    Dim lngHighWord As Long
  249.    Dim lngLowWord As Long
  250.    Dim lngOverflow As Long
  251.  
  252.    lngLowWord = (lngVal1 And &HFFFF&) _
  253.        + (lngVal2 And &HFFFF&) _
  254.        + (lngVal3 And &HFFFF&) _
  255.        + (lngVal4 And &HFFFF&)
  256.  
  257.    lngOverflow = lngLowWord \ 65536
  258.  
  259.    lngHighWord = (((lngVal1 And &HFFFF0000) \ 65536) _
  260.        + ((lngVal2 And &HFFFF0000) \ 65536) _
  261.        + ((lngVal3 And &HFFFF0000) \ 65536) _
  262.        + ((lngVal4 And &HFFFF0000) \ 65536) _
  263.        + lngOverflow) And &HFFFF&
  264.  
  265.    MD5LongAdd4 = MD5LongConversion((lngHighWord * 65536#) _
  266.        + (lngLowWord And &HFFFF&))
  267. End Function
  268.  
  269. Private Sub MD5Decode(intLength As Integer, _
  270.  lngOutBuffer() As Long, bytInBuffer() As Byte)
  271.    Dim intDblIndex As Integer
  272.    Dim intByteIndex As Integer
  273.    Dim dblSum As Double
  274.  
  275.    intDblIndex = 0
  276.  
  277.    For intByteIndex = 0 To intLength - 1 Step 4
  278.  
  279.        dblSum = bytInBuffer(intByteIndex) + bytInBuffer(intByteIndex + 1) _
  280.            * 256# + bytInBuffer(intByteIndex + 2) _
  281.            * 65536# + bytInBuffer(intByteIndex + 3) * 16777216#
  282.        lngOutBuffer(intDblIndex) = MD5LongConversion(dblSum)
  283.        intDblIndex = (intDblIndex + 1)
  284.  
  285.    Next intByteIndex
  286. End Sub
  287.  
  288. Private Function MD5LongConversion(dblValue As Double) As Long
  289.    If dblValue < 0 Or dblValue >= OFFSET_4 Then Error 6
  290.  
  291.    If dblValue <= MAXINT_4 Then
  292.        MD5LongConversion = dblValue
  293.    Else
  294.        MD5LongConversion = dblValue - OFFSET_4
  295.    End If
  296. End Function
  297.  
  298. Private Sub MD5Finish()
  299.    Dim dblBits As Double
  300.    Dim arrPadding(72) As Byte
  301.    Dim lngBytesBuffered As Long
  302.  
  303.    arrPadding(0) = &H80
  304.  
  305.    dblBits = lngTrack * 8
  306.  
  307.    lngBytesBuffered = lngTrack Mod 64
  308.  
  309.    If lngBytesBuffered <= 56 Then
  310.        MD564Split (56 - lngBytesBuffered), arrPadding
  311.    Else
  312.        MD564Split (120 - lngTrack), arrPadding
  313.    End If
  314.  
  315.    arrPadding(0) = MD5LongConversion(dblBits) And &HFF&
  316.    arrPadding(1) = MD5LongConversion(dblBits) \ 256 And &HFF&
  317.    arrPadding(2) = MD5LongConversion(dblBits) \ 65536 And &HFF&
  318.    arrPadding(3) = MD5LongConversion(dblBits) \ 16777216 And &HFF&
  319.    arrPadding(4) = 0
  320.    arrPadding(5) = 0
  321.    arrPadding(6) = 0
  322.    arrPadding(7) = 0
  323.  
  324.    MD564Split 8, arrPadding
  325. End Sub
  326.  
  327. Private Function MD5StringChange(lngnum As Long) As String
  328.    Dim bytA As Byte
  329.    Dim bytB As Byte
  330.    Dim bytC As Byte
  331.    Dim bytD As Byte
  332.  
  333.        bytA = lngnum And &HFF&
  334.        If bytA < 16 Then
  335.            MD5StringChange = "0" & Hex(bytA)
  336.        Else
  337.            MD5StringChange = Hex(bytA)
  338.        End If
  339.  
  340.        bytB = (lngnum And &HFF00&) \ 256
  341.        If bytB < 16 Then
  342.            MD5StringChange = MD5StringChange & "0" & Hex(bytB)
  343.        Else
  344.            MD5StringChange = MD5StringChange & Hex(bytB)
  345.        End If
  346.  
  347.        bytC = (lngnum And &HFF0000) \ 65536
  348.        If bytC < 16 Then
  349.            MD5StringChange = MD5StringChange & "0" & Hex(bytC)
  350.        Else
  351.            MD5StringChange = MD5StringChange & Hex(bytC)
  352.        End If
  353.  
  354.        If lngnum < 0 Then
  355.            bytD = ((lngnum And &H7F000000) \ 16777216) Or &H80&
  356.        Else
  357.            bytD = (lngnum And &HFF000000) \ 16777216
  358.        End If
  359.  
  360.        If bytD < 16 Then
  361.            MD5StringChange = MD5StringChange & "0" & Hex(bytD)
  362.        Else
  363.            MD5StringChange = MD5StringChange & Hex(bytD)
  364.        End If
  365. End Function
  366.  
  367. Private Function MD5Value() As String
  368.  
  369.    MD5Value = LCase(MD5StringChange(arrLongConversion(1)) & _
  370.        MD5StringChange(arrLongConversion(2)) & _
  371.        MD5StringChange(arrLongConversion(3)) & _
  372.        MD5StringChange(arrLongConversion(4)))
  373. End Function
  374.  
  375. Public Function CalculateMD5(strMessage As String) As String
  376.    Dim bytBuffer() As Byte
  377.  
  378.    bytBuffer = MD5StringArray(strMessage)
  379.  
  380.    MD5Start
  381.    MD564Split Len(strMessage), bytBuffer
  382.    MD5Finish
  383.  
  384.    CalculateMD5 = MD5Value
  385. End Function
  386.  
  387. Private Sub MD5Start()
  388.    lngTrack = 0
  389.    arrLongConversion(1) = MD5LongConversion(1732584193#)
  390.    arrLongConversion(2) = MD5LongConversion(4023233417#)
  391.    arrLongConversion(3) = MD5LongConversion(2562383102#)
  392.    arrLongConversion(4) = MD5LongConversion(271733878#)
  393. End Sub
  394.  

Con el modulo anterior en un Formualrio:

Código
  1. Private Sub Command1_Click()
  2.    Text2.Text = CalculateMD5(Text1.Text)
  3. End Sub
  4.  

De este ultimo desconozco los creditos...
Dulces Lunas


« Última modificación: 24 Junio 2009, 22:51 pm por ░▒▓BlackZeroҖ▓▒░ » En línea

The Dark Shadow is my passion.
Carloswaldo
Traductor
Moderador Global
***
Desconectado Desconectado

Mensajes: 4.764


Nos reservamos el derecho de ban.


Ver Perfil WWW
Re: Librería para calcular hashes y hmacs
« Respuesta #2 en: 18 Julio 2009, 06:54 am »

Gracias!

Pero lo que ahora necesito es algo para calcular hmacs.

http://en.wikipedia.org/wiki/HMAC

De hecho ahí mismo hay un pseudocódigo de cómo se podría hacer una implementación:

Código:
function hmac (key, message)
    opad = [0x5c * blocksize] // Where blocksize is that of the underlying hash function
    ipad = [0x36 * blocksize]

    if (length(key) > blocksize) then
        key = hash(key) // keys longer than blocksize are shortened
    end if

    for i from 0 to length(key) - 1 step 1
        ipad[i] = ipad[i] ⊕ key[i] // Where ⊕ is exclusive or (XOR)
        opad[i] = opad[i] ⊕ key[i]
    end for

    return hash(opad ++ hash(ipad ++ message)) // Where ++ is concatenation
end function

Lo que pasa es que no entiendo nada. xD ¿Se puede pasar ese código a vb6?
En línea





Dominio en venta: https://forojapones.com/
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Algoritmo para calcular logaritmos ?
Programación General
juancaa 3 6,648 Último mensaje 5 Marzo 2012, 15:32 pm
por criskapunk
[Resuelto] ¿Existe librería para calcular el tiempo pasado entre dos fechas?
PHP
Leguim 1 2,819 Último mensaje 7 Febrero 2020, 16:19 pm
por EdePC
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines