📄 mactripledes.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "MACTripleDES"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2006 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: MACTripleDES
'
''
' Represents a TripleDES implementation of an HMAC algorithm.
'
Option Explicit
Implements IObject
Implements HashAlgorithm
Implements KeyedHashAlgorithm
Implements ICryptoTransform
Private mTripleDES As TripleDES
Private mStream As CryptoStream
Private mHashStream As New SingleBlockStream
Private mDisposed As Boolean
Private mHashStarted As Boolean
''
' Returns the padding being used by the algorithm.
'
' @return The padding being used.
'
Public Property Get Padding() As PaddingMode
Call VerifyNotDisposed
Padding = mTripleDES.Padding
End Property
''
' Sets the padding to be used by the hash algorithm.
'
' @param RHS The new padding value.
'
Public Property Let Padding(ByVal RHS As PaddingMode)
Call VerifyNotDisposed
mTripleDES.Padding = RHS
End Property
''
' Returns the key being used by the hash algorithm.
'
' @return The key being used.
'
Public Property Get Key() As Byte()
Call VerifyNotDisposed
Key = mTripleDES.Key
End Property
''
' Sets the key to be used by the hash algorithm.
'
' @param RHS The new key to be used.
'
Public Property Let Key(ByRef RHS() As Byte)
Call VerifyNotDisposed
If mHashStarted Then _
Throw Cor.NewCryptographicException("Cannot change key after hash has started.")
Call SetKey(RHS)
Set mStream = Nothing
End Property
''
' Returns if the hash algorithm implementation can be reused once a hash has been computed.
'
' @returns True if the implementation can be reused, False otherwise.
'
Public Property Get CanReuseTransform() As Boolean
CanReuseTransform = True
End Property
''
' Returns if the hash algorithm implementation can processd multiple blocks of data at a time.
'
' @return True if multiple blocks of data can be processed at a time, False otherwise.
' @remarks The size of 1 block of data is defined by the InputBlockSize property.
'
Public Property Get CanTransformMultipleBlocks() As Boolean
CanTransformMultipleBlocks = True
End Property
''
' Returns the computed hash after all data has been processed.
'
' @return A byte array containing the computed hash.
' @remarks The size of the array will vary depending on the hash algorithm being performed.
'
Public Property Get Hash() As Byte()
Call VerifyNotDisposed
If mHashStarted Then _
Throw Cor.NewInvalidOperationException("Cannot get Hash until TransformFinalBlock is called.")
Hash = mHashStream.Hash
End Property
''
' Returns the size of the hash value in bits.
'
' @return The size of the hash value in bits.
'
Public Property Get HashSize() As Long
HashSize = 64
End Property
''
' Returns the size of the input block size allowed.
'
' @returns The number of bytes a single input block is.
' @remarks When calling the TransformBlock method, the InputCount must equal this value.
' If the hash's CanTransformMultipleBlocks returns True, then the InputCount can be
' larger than the InputBlockSize, but must be an even multiple of InputBlockSize.
'
Public Property Get InputBlockSize() As Long
InputBlockSize = 1
End Property
''
' Returns the size of the output block.
'
' @return The size of the output block.
' @remarks When calling the TransformBlock method, the return value will be 0 or a multiple of this value.
'
Public Property Get OutputBlockSize() As Long
OutputBlockSize = 1
End Property
''
' Releases any resources being held by the hash algorithm.
'
' @remarks Once this is called, the object is disposed and cannot be reused.
'
Public Sub Clear()
Set mStream = Nothing
Set mHashStream = Nothing
Set mTripleDES = Nothing
mDisposed = True
End Sub
''
' Computes the hash on a source in its entirety.
'
' @param Source The source of data compute the hash for. This can be a byte array or object implementing the Stream interface.
' @param Index The starting index in a byte array source. If the source is not a byte array, this argument is ignored.
' @param Count The number of bytes to compute the hash on. If the source is not a byte array, this argument is ignored.
' @return The hash value computed from the source provided.
' @remarks If the source is a Stream based object, then the stream is read from and the hash continuously computed until
' the stream no longer returns data.
' <p>If data has already been processed using TransformBlock, then that data will be included in the hashing process.</p>
'
Public Function ComputeHash(ByRef Source As Variant, Optional ByRef Index As Variant, Optional ByRef Count As Variant) As Byte()
Call VerifyNotDisposed
Call StartHash
Select Case VarType(Source)
Case vbByteArray: Call ComputeHashOnBytes(Source, Index, Count)
Case vbObject: Call ComputeHashOnStream(Source)
Case Else: Throw Cor.NewArgumentException("Invalid source. Must be a byte array or Stream object.", "Source")
End Select
Call EndHash
ComputeHash = mHashStream.Hash
End Function
''
' Returns if the Value is equal to this object instance.
'
' @param Value The value to compare against this object instance.
' @return True if they are considered equal, False otherwise.
'
Public Function Equals(ByRef Value As Variant) As Boolean
Equals = Object.Equals(Me, Value)
End Function
''
' Returns a pseudo-unique number identifying this instance.
'
' @return Pseudo-unique number identifying this instance.
'
Public Function GetHashCode() As Long
GetHashCode = ObjPtr(CUnk(Me))
End Function
''
' Resets a hashing object to begin hashing a new value.
'
' @remarks If Clear has been called, then the object cannot be re-initialized.
'
Public Sub Initialize()
Set mStream = Nothing
End Sub
''
' Returns a string representation of this object instance.
'
' @return String representing this instance.
'
Public Function ToString() As String
ToString = Object.ToString(Me, App)
End Function
''
' Continues to compute the hash value for blocks of data.
'
' @param InputBuffer The bytes to continue computing the hash from.
' @param InputOffset The index into the byte array to begin computing from.
' @param InputCount The number of bytes to be included in the hash computation.
' @param OutputBuffer The data after it has been processed. This will be the same as the input data, no changes are made.
' @param OutputOffset The starting index in the output buffer to place the processed data.
' @return The number of bytes that were processed.
' @remarks The OutputBuffer will contain the same plain text data as the input buffer. No transformation of the data
' is applied. The OutputBuffer array can be a Null array or the same array as the InputBuffer. If the InputBuffer is
' used as the OutputBuffer and the InputOffset equals the OutputOffset, no data is copied.
' <p>Once this method is called, the Hash property cannot be called until the TransformFinalBlock is called, finishing
' the hash computation.
'
Public Function TransformBlock(ByRef InputBuffer() As Byte, ByVal InputOffset As Long, ByVal InputCount As Long, ByRef OutputBuffer() As Byte, ByVal OutputOffset As Long) As Long
Call VerifyNotDisposed
Call StartHash
If Not cArray.IsNull(OutputBuffer) Then
' We won't bother to copy if the OutputBuffer and offset are the same as
' the InputBuffer and offset since this would be copying the same
' data over the top of itself.
If (SAPtr(InputBuffer) <> SAPtr(OutputBuffer)) Or (InputOffset <> OutputOffset) Then
Dim Result As Long
Result = VerifyArrayRange(SAPtr(OutputBuffer), OutputOffset, InputCount)
If Result <> NO_ERROR Then Call ThrowArrayRangeException(Result, "OutputBuffer", OutputOffset, "OutputBuffer", InputCount, "OutputCount")
Call CopyMemory(OutputBuffer(OutputOffset), InputBuffer(InputOffset), InputCount)
End If
End If
Call mStream.WriteBlock(InputBuffer, InputOffset, InputCount)
TransformBlock = InputCount
End Function
''
' Finalizes the hash computation by processing the last block of data.
'
' @param InputBuffer The bytes to finish the hash computation with.
' @param InputOffset The index into the byte array to begin hash computations.
' @param InputCount The number of bytes to be included in the final hash computation.
' @return A copy of the portion of the InputBuffer that was processed.
' @remarks The hash value is not returned by this method. To retrieve the final
' hash value, call the Hash property.
' <p>The Hash property can only be called after this method is called. If the Hash property
' is called before this method, then an exception is thrown.</p>
' <p>If the implementation can be reused (CanReuseTransform) then the implemntation is reset
' to allow computing of a new hash value.</p>
'
Public Function TransformFinalBlock(ByRef InputBuffer() As Byte, ByVal InputOffset As Long, ByVal InputCount As Long) As Byte()
Call VerifyNotDisposed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -