One Way Hash of a String Suite

إمتدادا للموضوع الموجود في اللينك التالي

تم تطوير الكلاس الموجود في اللينك أعلاه لكي يدعم أكثر من طريقة للتشفير و هنا يمكن لمستخدم الكلاس أن يختار من بين واحدة من ثلاثة طرق للتشفير وهي كالتالي

SHA512
SHA384
SHA256


الكلاس الجديد تمت كتابته بحيث لا يمكن تمريره من خلال الكود عن طريق تعريف متغير يعبر عنه
بل هو يسمح فقط بتمرير النوع المراد استخدامه في النشفير من خلال دالة عامة واحدة اسمها


 GetSuite
ومن خلالها يتم تمرير طريقة التشفير هلي هيئة

 Enum

الكود التالي يوضح شكل الكلاس


Imports System.Security.Cryptography
Imports System
.TextPublic MustInherit Class HashSuite#Region "Field"
 
   Private Const hashLength As Integer 8#End Region

#Region "Property"
 
   Friend MustOverride Property Algorithm As HashAlgorithm
    Friend ReadOnly Property SuiteName 
As String
        Get
            Return MyBase
.GetType().Name.Replace("Suite"String.Empty)
 
       End Get
    End Property
#End Region

#Region "Method"
 
   Protected Friend MustOverride Function Encrypt(source As String) As String

    Protected Friend 
Function Verify(source As Stringhashed As String) As Boolean
        Return Verify
(Encoding.UTF8.GetBytes(Encrypt(source)), Encoding.UTF8.GetBytes(hashed))
 
   End Function

 
   Public Shared Function GetSuite(kind As SuiteKind) As Func(Of HashSuite)
 
       Select Case kind
            Case SuiteKind
.SHA512
                Return 
Function() GetSuites()(CInt(SuiteKind.SHA512))()
 
           Case SuiteKind.SHA384
                Return 
Function() GetSuites()(CInt(SuiteKind.SHA384))()

 
           Case SuiteKind.SHA256
                Return 
Function() GetSuites()(CInt(SuiteKind.SHA256))()
 
       End Select
        Return Nothing
    End 
Function

 
   Private Shared Iterator Function GetSuites() As IEnumerable(Of Func(Of HashSuite))
 
       Yield Function() New SHA256ManagedSuite
        Yield 
Function() New SHA384ManagedSuite
        Yield 
Function() New SHA512ManagedSuite
        Return
    End 
Function

 
   Private Function Encrypt(provider As HashAlgorithmsource As String) As String
        Dim data 
As Byte() = provider.ComputeHash(Encoding.UTF8.GetBytes(source))

 
       For i As Integer hashLength To data.Length 1 Step hashLength
            For j 
As Integer 0 To hashLength 1
                data
(j) = (data(j) Xor data(i))
 
           Next
        Next
        Return BitConverter
.ToString(data0hashLength).Replace("-"String.Empty)

 
   End Function

 
   Private Function Verify(a() As Byteb() As Byte) As Boolean
        Dim length 
As Integer a.Length Xor b.Length
        Dim i 
As Integer 0
        While i 
a.Length AndAlso i b.Length
            length 
length Or a(i) Xor b(i)
 
           i += 1
        End 
While
 
       Return length 0
    End 
Function#End Region

#Region "SHA512ManagedSuite"

 
   Private Class SHA512ManagedSuite
        Inherits HashSuite
#Region "Field"
 
       Private internalHasher As HashAlgorithm#End Region

#Region "Property"
 
       Friend Overrides Property Algorithm As HashAlgorithm
            Get
                Dim hasher 
As HashAlgorithm internalHasher
                If hasher Is Nothing Then
                    hasher 
= New SHA512Managed
                    Algorithm 
hasher
                End 
If
 
               Return hasher
            End Get
            Set
(value As HashAlgorithm)
 
               internalHasher value
            End Set
        End Property
#End Region

#Region "Method"
 
       Protected Friend Overrides Function Encrypt(source As String) As String
            Return MyBase
.Encrypt(Algorithmsource)
 
       End Function#End Region

 
   End Class ' SHA512ManagedSuite

#End Region

#Region "SHA384ManagedSuite"
    Private Class SHA384ManagedSuite
        Inherits HashSuite

#Region "Field"
        Private internalHasher As HashAlgorithm
#End Region

#Region "Property"
        Friend Overrides Property Algorithm As HashAlgorithm
            Get
                Dim hasher As HashAlgorithm = internalHasher
                If hasher Is Nothing Then
                    hasher = New SHA384Managed
                    Algorithm = hasher
                End If
                Return hasher
            End Get
            Set(value As HashAlgorithm)
                internalHasher = value
            End Set
        End Property
#End Region

#Region "Method"
        Protected Friend Overrides Function Encrypt(source As String) As String
            Return MyBase.Encrypt(Algorithm, source)
        End Function
#End Region

    End Class ' 
SHA384ManagedSuite#End Region

#Region "SHA256ManagedSuite"

 
   Private Class SHA256ManagedSuite
        Inherits HashSuite
#Region "Field"
 
       Private internalHasher As HashAlgorithm#End Region

#Region "Property"
 
       Friend Overrides Property Algorithm As HashAlgorithm
            Get
                Dim hasher 
As HashAlgorithm internalHasher
                If hasher Is Nothing Then
                    hasher 
= New SHA256Managed
                    Algorithm 
hasher
                End 
If
 
               Return hasher
            End Get
            Set
(value As HashAlgorithm)
 
               internalHasher value
            End Set
        End Property
#End Region

#Region "Method"
 
       Protected Friend Overrides Function Encrypt(source As String) As String
            Return MyBase
.Encrypt(Algorithmsource)
 
       End Function#End Region

 
   End Class ' SHA256ManagedSuite

#End Region

End Class ' 
HashSuitePublic Enum SuiteKind
    SHA512 
0
    SHA384 
1
    SHA256 
2
End Enum 

Download & Usage



Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET