Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

a tiny-mfa implementation written in go

License

NotificationsYou must be signed in to change notification settings

ghmer/go-tiny-mfa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents

Overview

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.

Installation

To install the Tiny MFA package, run the following command:

go get github.com/ghmer/go-tiny-mfa

Usage

Generating TOTP Tokens

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)}

Verifying TOTP Tokens

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)}

Generating QR Codes

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")}

Encrypting and Decrypting Data

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)}

API Documentation

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.

License

The Tiny MFA package is released under the MIT License. SeeLICENSE for details.


[8]ページ先頭

©2009-2025 Movatter.jp