Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Util Plugin, MBS Encryption Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/HMAC Example
This example is the version from Fri, 25th Jun 2015.
Project "HMAC Example.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
Test_HMAC_MD5 "", "", "74e6f7298a9c2d168935f58c001bad88"
Test_HMAC_SHA1 "", "", "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"
Test_HMAC_SHA256 "", "", "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad"
Test_HMAC_MD5 "key", "The quick brown fox jumps over the lazy dog", "80070713463e7749b90c2dc24911e275"
Test_HMAC_SHA1 "key", "The quick brown fox jumps over the lazy dog", "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"
Test_HMAC_SHA256 "key", "The quick brown fox jumps over the lazy dog", "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
End EventHandler
Function CalcSHA1MBS(data as string) As string
dim s as new SHA1MBS // in Encryption Plugin
s.Add data
Return s.Result
End Function
Function CalcSHA256MBS(data as string) As string
dim s as new SHA256MBS
s.Add data
Return s.Result
End Function
Function HMAC_MD5(key as string, message as string) As string
const blocksize = 64
message = DefineEncoding(message, nil) // remove encoding
if lenb(key) > blocksize then
key = MD5MBS(key) // in Encryption plugin
end if
while lenb(key) < blocksize
key = key + chrb(0)
wend
dim okey as string
dim ikey as string
for i as integer = 1 to blocksize
okey = okey + chrb(&h5C)
next
for i as integer = 1 to blocksize
ikey = ikey + chrb(&h36)
next
ikey = StringXORMBS(ikey, key)
okey = StringXORMBS(okey, key)
Return MD5MBS( okey + MD5MBS(ikey + message))
End Function
Function HMAC_SHA1(key as string, message as string) As string
const blocksize = 64
message = DefineEncoding(message, nil) // remove encoding
if lenb(key) > blocksize then
key = CalcSHA1MBS(key)
end if
while lenb(key) < blocksize
key = key + chrb(0)
wend
dim okey as string
dim ikey as string
for i as integer = 1 to blocksize
okey = okey + chrb(&h5C)
next
for i as integer = 1 to blocksize
ikey = ikey + chrb(&h36)
next
ikey = StringXORMBS(ikey, key)
okey = StringXORMBS(okey, key)
Return CalcSHA1MBS( okey + CalcSHA1MBS(ikey + message))
End Function
Function HMAC_SHA256(key as string, message as string) As string
const blocksize = 64
message = DefineEncoding(message, nil) // remove encoding
if lenb(key) > blocksize then
key = CalcSHA256MBS(key)
end if
while lenb(key) < blocksize
key = key + chrb(0)
wend
dim okey as string
dim ikey as string
for i as integer = 1 to blocksize
okey = okey + chrb(&h5C)
next
for i as integer = 1 to blocksize
ikey = ikey + chrb(&h36)
next
ikey = StringXORMBS(ikey, key)
okey = StringXORMBS(okey, key)
Return CalcSHA256MBS( okey + CalcSHA256MBS(ikey + message))
End Function
Sub Test_HMAC_MD5(key as string, message as string, ExpectedResult as string)
dim m as string = HMAC_MD5(key, message)
dim h as string = EncodeHex(m)
if h = ExpectedResult then
log CurrentMethodName+" OK"
else
log CurrentMethodName+" Failed: "+h+" ≠ "+ExpectedResult
end if
End Sub
Sub Test_HMAC_SHA1(key as string, message as string, ExpectedResult as string)
dim m as string = HMAC_SHA1(key, message)
dim h as string = EncodeHex(m)
if h = ExpectedResult then
log CurrentMethodName+" OK"
else
log CurrentMethodName+" Failed: "+h+" ≠ "+ExpectedResult
end if
End Sub
Sub Test_HMAC_SHA256(key as string, message as string, ExpectedResult as string)
dim m as string = HMAC_SHA256(key, message)
dim h as string = EncodeHex(m)
if h = ExpectedResult then
log CurrentMethodName+" OK"
else
log CurrentMethodName+" Failed: "+h+" ≠ "+ExpectedResult
end if
End Sub
Sub log(s as string)
MainWindow.List.AddRow s
End Sub
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
End Class
End Project
The items on this page are in the following plugins: MBS Util Plugin.