Quick actions

cmd+k|ctrl+k

Navigation

Languages

Testing

Snippet info

Language

Go

Visibility

public

Author

ubaidillah.hf

Created

2022-12-09T10:35:29.085208Z

Updated

2022-12-09T10:35:48.508602Z

package main

import (
    "fmt"
    "strconv"
    "math/rand"
    "crypto/sha256"
    "time"
)

func GenerateCode() string {
	// Get the current time in nanoseconds.
	now := time.Now().UnixNano()

	// Generate a random number using the math/rand function.
	rand.Seed(time.Now().UnixNano())
	randNum := rand.Int63() % 1000000

	// Combine the current time, the random number, and a hash of the current time
	// to create the OTP.
	hash := sha256.New()
	hash.Write([]byte(strconv.FormatInt(now, 10)))
	otp := strconv.FormatInt(now, 10)[:6] + strconv.FormatInt(randNum, 10)[:6] + fmt.Sprint(hash.Sum(nil)[:6])

	// Return the OTP.
	return otp
}

func main() {
    fmt.Println(GenerateCode())
}
INFO