' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Security.Cryptography
Imports System.Text

Namespace Encrypt
    Public Class TestPasswordHashing
        Private Const PREFIX As String = "pbkdf2"
        ' generate salt
        Private Shared rng As RandomNumberGenerator = RandomNumberGenerator.Create()
        Private Shared Function GenSalt(size As Integer) As Byte()
            Dim res As Byte() = New Byte(size - 1) {}
            rng.GetBytes(res)
            Return res
        End Function
        ' hash: Byte() + Byte() salt -> Byte()
        Public Shared Function Hash(pw As Byte(), salt As Byte(), nit As Integer, keylen As Integer) As Byte()
            Dim alg As New Rfc2898DeriveBytes(pw, salt, nit)
            Return alg.GetBytes(keylen \ 8)
        End Function
        ' Base64 encode: Byte() -> String
        ' Base64 decode: String -> Byte()
        Private Shared Function Encode(b As Byte()) As String
            Return Convert.ToBase64String(b)
        End Function
        Private Shared Function Decode(s As String) As Byte()
            Return Convert.FromBase64String(s)
        End Function
        ' hash: String -> Base64 String
        ' verify: String + saved hash String -> Boolean
        Public Shared Function Hash(pw As String, saltlen As Integer, nit As Integer, keylen As Integer) As String
            Dim salt As Byte() = GenSalt(saltlen)
            Return [String].Format("{0}${1}${2}${3}${4}", PREFIX, nit, keylen, Encode(salt), Encode(Hash(Encoding.UTF8.GetBytes(pw), salt, nit, keylen)))
        End Function
        Public Shared Function Verify(pw As String, savedhash As String) As Boolean
            Dim parts As String() = savedhash.Split("$"C)
            If parts(0) <> PREFIX Then
                Throw New Exception("Unsupported algorithm: " & parts(0))
            End If
            Dim nit As Integer = Integer.Parse(parts(1))
            Dim keylen As Integer = Integer.Parse(parts(2))
            Dim salt As Byte() = Decode(parts(3))
            Return Encode(Hash(Encoding.UTF8.GetBytes(pw), salt, nit, keylen)) = parts(4)
        End Function
        ' test
        Private Const PW As String = "A super secret password"
        Private Shared Sub Test(label As String, saltlen As Integer, nit As Integer, keylen As Integer)
            Console.WriteLine(label & ":")
            Dim hashval As String = Hash(PW, saltlen, nit, keylen)
            Console.WriteLine("  " & hashval)
            Dim ok As Boolean = Verify(PW, hashval)
            Console.WriteLine("  " & ok)
            Dim ok2 As Boolean = Verify(PW & "X", hashval)
            Console.WriteLine("  " & ok2)
        End Sub
        Private Shared Sub Test2()
            If Not Verify(PW, "pbkdf2$10000$512$OPf3qDpgi7iIyQ/Cn2XUHQ==$3T8Yhih2SEq22bnbhCucHs9TTQmsSz28O8jk2cvYh6ihZhNHBcOXsUlX2UAKWJEdmjAA+R8yMOAqtJYdSfqYwg==") Then
                Throw New Exception("Ooops")
            End If
        End Sub
        Public Shared Sub Main(args As String())
            Test("PBKDF2 with HMAC SHA-1", 16, 10000, 512)
            Test2()
        End Sub
    End Class
End Namespace
