Code-Beispiel
HMAC-SHA1 Token (Time-based)
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
k. A. | Neo7530 | 07.11.2012 |
Das ist ein Token-Programm nach HMAC-SHA1, so wie er z.B. vom Google Authenticator benutzt wird.
Einfach am Ende des Codes die BASE32 Codierten Strings eintragen und bei Bedarf den Token berechnen lassen. Berechnet wird das ganze mit dem Unix-Timestamp (MEZ -1 Stunde) evtl anpassen. Der Timestamp wird komplett händisch erzeugt. Es wird ausserdem die SHA1-Bibliothek benötigt (hier in der Codebase zum Download).
Ich benutze das Programm momentan, und die benötigten OTP's haben bisher immer gepasst... ;)
Viel Spass damit.
#Include "SHA1Checksum.bas"
Screen 10
Declare Function hmac(tokentime As Integer) As Integer
Declare Function unixzeit(jahr As Integer,monat As Integer, tag As Integer, stunde As Integer, Minute As Integer, sekunde As Integer) As Integer
Declare Function base32dec(text As String) As String
Declare FUNCTION removespace(text As string) As String
Dim Shared As String secret
'Dim Shared As Integer laenge
? "Zeitbasiertes One-Time-Pad nach HMAC-SHA1. (Google,Dropbox,Wordpress,SSHD)"
Do
Locate 5,1
Dim MONAT as Integer
MONAT = Val(Mid(Date,1,2))
Dim TAG as Integer
TAG = Val(Mid(Date,4,5))
Dim JAHR as Integer
JAHR = Val(Mid(Date,7,10))
Dim STUNDE as Integer
STUNDE = Val(Mid(time,1,2))
Dim MINUT as Integer
MINUT = Val(Mid(Time,4,5))
Dim SEKUNDE as Integer
SEKUNDE = Val(Mid(Time,7,8))
Dim UTC As Integer = stunde -1
? "MEZ: ";Time
? "Datum: ";Date
?
Dim unixtime As Integer = unixzeit(JAHR,MONAT,TAG,UTC,MINUT,SEKUNDE)
Dim tokentime As Integer = Int((unixzeit(JAHR,MONAT,TAG,UTC,MINUT,SEKUNDE))/30)
? Using "UNIX-Zeit: ##########";unixtime
? Using "TOKEN-Zeit: ##########";tokentime
?
Restore dropbox
Read secret
secret = base32dec(removespace(secret))
? Using "Dropbox-TOKEN: ######";hmac(tokentime)
Restore vdr
Read secret
secret = base32dec(removespace(secret))
? Using "VDR-TOKEN: ######";hmac(tokentime)
Restore google
Read secret
secret = base32dec(removespace(secret))
? Using "Google TOKEN: ######";hmac(tokentime)
Sleep 1000
Loop Until MultiKey(1)
End
FUNCTION removespace(text As string) As String
Dim ntext As String
FOR i As Integer = 0 To LEN(Text)
IF MID(Text,i,1) <> " " THEN ntext=ntext+MID(Text,i,1)
Next
Return ntext
END Function
Function base32dec(text As String) As String
Const As String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
Dim As Integer i,k
Dim As Integer p
Dim As LongInt nr
Dim As String result, result1
Dim As String*1 txt
text = UCase(text)
If Len(text) Mod 8 > 0 Then
For i = 1 To 8 - (Len(text) Mod 8)
text += "="
Next
EndIf
result = ""
nr = 0
For k = 0 To Len(text)/8 -1
result = ""
For i = k*8 to k*8 +7
txt = text[i]
p = InStr(1,alphabet,txt)
If p > 0 Then
nr = nr * 32 + (p - 1)
Else
nr = nr * 32
EndIf
' ? nr
Next
For i = 0 To Len(text) / 2
result += chr(nr mod 256)
nr = nr \ 256
Next
For i = 4 To 0 Step -1
result1 += Chr(result[i])
Next
Next
Return result1
End Function
Function unixzeit(jahr As Integer,monat As Integer, tag As Integer, stunde As Integer, Minute As Integer, sekunde As Integer) As Integer
Dim unix_zeit As integer, jahre As integer, schaltjahre As Integer
Static As Integer tage_bis_monatsanfang(0 To 11) = {0,31,59,90,120,151,181,212,243,273,304,334}
jahre=jahr-1970
schaltjahre=((jahr-1)-1968)/4 - ((jahr-1)-1900)/100 + ((jahr-1)-1600)/400
unix_zeit=sekunde + 60 * Minute + 60*60*(stunde) + (tage_bis_monatsanfang(monat-1)+tag-1)*60*60*24 + (jahre*365+schaltjahre)*60*60*24
If ( (monat<3) And (jahr Mod 4 = 0 AND (jahr Mod 100 > 0 OR jahr Mod 400 = 0)) ) Then unix_zeit-=60*60*24
Return unix_zeit
End Function
Function hmac(tokentime As Integer) As Integer
Dim As Integer totp, offset
Dim As UByte int2str
Dim As String plaintext, tktime_st, hash_st, plaintext1, ipad_hash, fhash_st
Dim As UByte ipad(63) = {_
&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,_
&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,_
&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,_
&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36,&h36_
}
Dim As UByte opad(63) = {_
&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,_
&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,_
&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,_
&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c,&h5c_
}
For i As Integer = 0 To Len(secret)
ipad(i) = ipad(i) Xor secret[i]
opad(i) = opad(i) Xor secret[i]
Next
For i As Integer = 0 To 63
plaintext += Chr(ipad(i))
Next
int2str = (tokentime Shr 24)
tktime_st += Chr(int2str)
int2str = (tokentime Shr 16)
tktime_st += Chr(int2str)
int2str = (tokentime Shr 8)
tktime_st += Chr(int2str)
int2str = tokentime
tktime_st += Chr(int2str)
For i As Integer = 0 To 3
plaintext += Chr(&h00)
Next
plaintext += tktime_st
ipad_hash = createSHA1(plaintext)
For i As Integer = 0 To 4
int2str = (hash(i) Shr 24)
hash_st += Chr(int2str)
int2str = (hash(i) Shr 16)
hash_st += Chr(int2str)
int2str = (hash(i) Shr 8)
hash_st += Chr(int2str)
int2str = hash(i)
hash_st += Chr(int2str)
Next
For i As Integer = 0 To 63
plaintext1 += Chr(opad(i))
Next
plaintext1 += hash_st
ipad_hash = createSHA1(plaintext1)
For i As Integer = 0 To 4
int2str = (hash(i) Shr 24)
fhash_st += Chr(int2str)
int2str = (hash(i) Shr 16)
fhash_st += Chr(int2str)
int2str = (hash(i) Shr 8)
fhash_st += Chr(int2str)
int2str = hash(i)
fhash_st += Chr(int2str)
Next
offset= fhash_st[19] And &h0F
totp = (fhash_st[offset]) Shl 24 Or (fhash_st[offset+1]) Shl 16 Or (fhash_st[offset+2]) Shl 8 Or (fhash_st[offset+3])
totp and= &h7FFFFFFF
totp Mod= 1000000
Return totp
End Function
google:
Data "knp 56vk xnafg esa aak"
dropbox:
Data "das ist nen test 234567"
vdr:
Data "AUCH GROSS GEHT DAS"
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|