Skip to content

One-time password (OTP) implementation in Shell, Python and Go

homepage-banner

Introduction

One-time password (OTP) is a security mechanism that generates a unique password for each login attempt. It adds an extra layer of security to user accounts as the passwords expire immediately after use. OTP implementation can be done in different programming languages, including Shell, Python, and Go. In this article, we will discuss how OTP can be implemented in these languages.

OTP Implementation in Python

install pyotp

pip install pyotp

demo python code

import pyotp
totp = pyotp.TOTP('XXXXXXXX')
print(totp.now())

OTP Implementation in Go

install go-otp

go get github.com/xlzd/gotp

demo go code

package main

import (
    "fmt"
    "github.com/xlzd/gotp"
)

func main() {
    fmt.Println("Current OTP is", gotp.NewDefaultTOTP("XXXXXXXX").Now())
}

OTP Implementation in Shell

install oathtool

## Debian & Ubuntu
apt install oathtool

## CentOS & Fedora
sudo yum -y install oathtool
sudo dnf -y install oathtool

## Mac 
brew install oath-toolkit

demo command

oathtool -b --totp XXXXXXXXX

Conclusion

OTP is a simple and effective security mechanism that can be implemented in different programming languages. In this article, we discussed how OTP can be implemented in Shell, Python, and Go. While the implementation may vary, the basic principle remains the same: generating a unique password that expires immediately after use.

Reference

  • https://github.com/pquerna/otp
  • https://github.com/xlzd/gotp
  • https://www.nongnu.org/oath-toolkit/
Leave a message







Disclaimer
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback me#imzye.com, replace # with @