Attribute VB_Name = "basPBKDF2"
Option Explicit
Option Base 0

Public Function Test_PBKDF2()

' This example uses PBKDF2 as defined in PKCS #5 v2.0
' from test vectors provided by Dr. Stephen Henson (drh@celocom.com),
' The password is "password" (without quotes).
' This derived key is for the 'des-ede3-cbc' example in the test vectors
' and therefore needs to be 24 bytes long.

    Dim abDerivedKey() As Byte
    Dim lngKeyLen As Long
    Dim sPassword As String
    Dim abPwdBytes() As Byte
    Dim abSalt(7) As Byte
    Dim lngCount As Long
    Dim lngRet As Long
    
' Convert password String to an array of Bytes
    sPassword = "password"
    abPwdBytes = StrConv(sPassword, vbFromUnicode)
    
' Set 8-byte salt = 78 57 8E 5A 5D 63 CB 06
    abSalt(0) = &H78
    abSalt(1) = &H57
    abSalt(2) = &H8E
    abSalt(3) = &H5A
    abSalt(4) = &H5D
    abSalt(5) = &H63
    abSalt(6) = &HCB
    abSalt(7) = &H6
    
' Iteration count is 2048
    lngCount = 2048
    
' Pre-dimension output for derived key to required length of 24 bytes
' (Don't forget to do this)
    lngKeyLen = 24
    ReDim abDerivedKey(lngKeyLen - 1)
    
' Derive PBKDF2 key using function from CryptoSys
    lngRet = PBE_Kdf2(abDerivedKey(0), lngKeyLen, abPwdBytes(0), Len(sPassword), abSalt(0), 8&, lngCount, 0&)
    
' Convert bytes to hex and print
    Debug.Print "Derived key = " & cv_HexFromBytes(abDerivedKey)
'Correct Derived key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643

End Function

Public Function Test_PBKDF2_Longer()

    Dim abDerivedKey() As Byte
    Dim lngKeyLen As Long
    Dim sPassword As String
    Dim abPwdBytes() As Byte
    Dim abSalt(7) As Byte
    Dim lngCount As Long
    Dim lngRet As Long
    
' Convert password String to an array of Bytes
    sPassword = "password"
    abPwdBytes = StrConv(sPassword, vbFromUnicode)
    
' Set 8-byte salt = 78 57 8E 5A 5D 63 CB 06
    abSalt(0) = &H78
    abSalt(1) = &H57
    abSalt(2) = &H8E
    abSalt(3) = &H5A
    abSalt(4) = &H5D
    abSalt(5) = &H63
    abSalt(6) = &HCB
    abSalt(7) = &H6
    
' Iteration count is 2048
    lngCount = 2048
    
' Pre-dimension output for derived key to required length of 24 bytes
' (Don't forget to do this)
    lngKeyLen = 64
    ReDim abDerivedKey(lngKeyLen - 1)
    
' Derive PBKDF2 key using function from CryptoSys
    lngRet = PBE_Kdf2(abDerivedKey(0), lngKeyLen, abPwdBytes(0), Len(sPassword), abSalt(0), 8&, lngCount, 0&)
    
' Convert bytes to hex and print
    Debug.Print "Derived key = " & cv_HexFromBytes(abDerivedKey)

End Function