IValidator.Init
Dim keyNode As XmlNode = config.SelectSingleNode( "key" )
_key = ExtractKeyFromNode (keyNode)
If _key IsNothingThen 'Throw a cryptographic exception EndIf End Sub
()
Dim key AsByte() =Nothing If keyNode IsNothingThen ReturnNothing EndIf Try key = Convert.FromBase64String(keyNode.InnerText)
Catch e As Exception
'exception handling code EndTry Return key
End Function
_
Implements IValidator.Sign
Dim outSignature AsByte() =Nothing Dim fs As FileStream =Nothing Try fs =New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim kha As KeyedHashAlgorithm = KeyedHashAlgorithm.Create()
kha.Key = Convert.FromBase64String(key)
outSignature = kha.ComputeHash(fs)
Catch e As Exception
' exception handling code Finally IfNot (fs IsNothing) Then fs.Close()
EndIf EndTry Return Convert.ToBase64String(outSignature)
End Function _
Implements IValidator.Sign
Dim outSignature AsByte() =Nothing Dim xmlNodeByte AsByte() =Nothing xmlNodeByte = Encoding.Unicode.GetBytes(Xml.InnerXml)
Try Dim kha As KeyedHashAlgorithm = KeyedHashAlgorithm.Create()
kha.Key = Convert.FromBase64String(key)
outSignature = kha.ComputeHash(xmlNodeByte)
Catch e As Exception
'exception handling code EndTry Return Convert.ToBase64String(outSignature)
End Function
) _
AsBoolean If firstKey.Length <> secondKey.Length Then ReturnFalse EndIf Dim i AsInteger For i =0To firstKey.Length -1 If firstKey(i) <> secondKey(i) Then ReturnFalse EndIf Next i
ReturnTrue End Function
[C#]
privatebool compareKeys( byte[] firstKey, byte[] secondKey )
{
if( firstKey.Length != secondKey.Length ) returnfalse;
for( int i =0 ; i < firstKey.Length; i++ )
{
if( firstKey[ i ] != secondKey[ i ] ) returnfalse;
} returntrue;
}