- Notifications
You must be signed in to change notification settings - Fork0
a tiny-mfa implementation written in go
License
ghmer/go-tiny-mfa
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The Tiny MFA package is a Go library for generating and verifying Time-Based One-Time Passwords (TOTP) according to the TOTP algorithm specified in RFC 6238. It also includes functionality for encrypting and decrypting data using AES-256-CBC.
To install the Tiny MFA package, run the following command:
go get github.com/ghmer/go-tiny-mfa
To generate a TOTP token, you can use theGenerateValidToken
method of theTinyMfa
struct. This method takes four arguments: the current Unix timestamp, the secret key, the offset type (either present, future, or past), and the desired token length.
package mainimport ("github.com/ghmer/go-tiny-mfa")funcmain() {tinymfa:=tinymfa.NewTinyMfa()timeStamp:=time.Now().Unix()key:= []byte("your_secret_key")// replace with your secret keysize:=6// replace with the totp lengthtoken,_:=tmfa.GenerateValidToken(timeStamp,&key,tinymfa.Present,size)iferr!=nil {panic(err) }fmt.Println(token)}
To verify a TOTP token, you can use theValidateToken
method of theTinyMfa
struct. This method takes five arguments: the submitted token, the secret key, the current Unix timestamp, and the desired token length.
package mainimport ("github.com/ghmer/go-tiny-mfa")funcmain() {tinymfa:=tinymfa.NewTinyMfa()tokenNow:="123456"// replace with the submitted tokenkey:= []byte("your_secret_key")// replace with your secret keytimeStamp:=time.Now().Unix()size:=6// replace with the totp lengthvalid,_:=tmfa.ValidateToken(tokenNow,&key,timeStamp,6)iferr!=nil {panic(err) }fmt.Println(valid)}
funcmain() {varissuerstring="tinymfa.parzival.link"varuserstring="demo"varkeystring=base32.StdEncoding.EncodeToString(Key)vardigitsuint8=6qrcode,err:=tmfa.GenerateQrCode(issuer,user,&key,digits)iferr!=nil {panic(err) }// write png to fileos.WriteFile("./qrcode1.png",qrcode,0644)// shorthand for the abovetmfa.WriteQrCodeImage(issuer,user,&key,digits,"./qrcode2.png")}
To encrypt data using theTinyMfa
package, you can use theEncrypt
method. This method takes two arguments: the data to be encrypted and the passphrase used for encryption.
package mainimport ("github.com/ghmer/go-tiny-mfa/utils")funcmain() {data:= []byte("Hello, World!")passphrase:= []byte("your_passphrase")encryptedData,err:=util.Encrypt(&data,&passphrase)iferr!=nil {panic(err) }fmt.Println(encryptedData)}
To decrypt data using theTinyMfa
package, you can use theDecrypt
method. This method takes two arguments: the encrypted data and the passphrase used for decryption.
package mainimport ("github.com/ghmer/go-tiny-mfa/utils")funcmain() {encryptedData:= []byte("your_encrypted_data")passphrase:= []byte("your_passphrase")decryptedData,err:=util.Decrypt(encryptedData,&passphrase)iferr!=nil {panic(err) }fmt.Println(decryptedData)}
TheTinyMfa
package includes the following methods:
GenerateValidToken
: Generates a TOTP token for the given timestamp and secret key.ValidateToken
: Verifies whether the submitted token is valid for the given timestamp and secret key.Encrypt
: Encrypts data using AES-256-CBC with the given passphrase.Decrypt
: Decrypts encrypted data using AES-256-CBC with the given passphrase.
The Tiny MFA package is released under the MIT License. SeeLICENSE for details.
About
a tiny-mfa implementation written in go