Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "crypto/sha1"
- "encoding/hex"
- "fmt"
- "os"
- "os/exec"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- // Function getOTP gerates a time based OTP and returns a 64bit Integer
- // or an error, if any.
- func getOTP() (int64, error) {
- currentTime := time.Now()
- UTCTime := time.Date(
- currentTime.Year(),
- currentTime.Month(),
- currentTime.Day(),
- currentTime.Hour(),
- 0,
- 0,
- 0,
- time.UTC)
- // Converting timestamp to string
- timestampString := strconv.FormatInt(UTCTime.Unix(), 10)
- // Create a SHA1 instance
- shaHash := sha1.New()
- // Write timestamp string to SHA1 instance
- shaHash.Write([]byte(timestampString))
- // Convert SHA1 Hash to String
- shaHashString := hex.EncodeToString(shaHash.Sum(nil))
- // Creating a Regular Expression instance to find digits
- // from the SHA1 hash
- regEx := regexp.MustCompile("\\d")
- // Extracting 8 digits
- digitsArr := regEx.FindAllString(shaHashString, -1)[0:8]
- return strconv.ParseInt(
- strings.Join(digitsArr[0:8], ""), // Join array to form a string
- 10, // base 10 numeral
- 64) // Int64
- }
- // Function launch simply launches a hardcoded command/application
- func launch() {
- // Create a command instance to be executed.
- cmd := exec.Command("app.dll")
- // Run the command.
- err := cmd.Run()
- if err != nil {
- fmt.Println("An error occured!")
- os.Exit(0)
- }
- }
- // Function main is the entry point of our application.
- func main() {
- // Print the message to ask for OTP
- fmt.Println("Enter OTP to launch")
- fmt.Println("---------------------")
- var otp int64 // To store entered OTP
- _, err := fmt.Scan(&otp) // Enter the OTP
- // Check if we've got any errors.
- if err != nil {
- fmt.Println("An error occured!")
- // Quit in case of any error.
- os.Exit(0)
- }
- // Find the correct OTP that we need to match
- // the user input against.
- realOTP, _ := getOTP()
- // Compare both real OTP and user's input
- if otp == realOTP {
- fmt.Println("Launching ...")
- // Launch the application if OTP is correct.
- launch()
- } else {
- fmt.Println("Wrong OTP!")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement