SHARE
TWEET

Untitled

a guest Sep 18th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "crypto/sha1"
  5.     "encoding/hex"
  6.     "fmt"
  7.     "os"
  8.     "os/exec"
  9.     "regexp"
  10.     "strconv"
  11.     "strings"
  12.     "time"
  13. )
  14.  
  15. // Function getOTP gerates a time based OTP and returns a 64bit Integer
  16. // or an error, if any.
  17. func getOTP() (int64, error) {
  18.  
  19.     currentTime := time.Now()
  20.  
  21.     UTCTime := time.Date(
  22.         currentTime.Year(),
  23.         currentTime.Month(),
  24.         currentTime.Day(),
  25.         currentTime.Hour(),
  26.         0,
  27.         0,
  28.         0,
  29.         time.UTC)
  30.  
  31.     // Converting timestamp to string
  32.     timestampString := strconv.FormatInt(UTCTime.Unix(), 10)
  33.  
  34.     // Create a SHA1 instance
  35.     shaHash := sha1.New()
  36.  
  37.     // Write timestamp string to SHA1 instance
  38.     shaHash.Write([]byte(timestampString))
  39.  
  40.     // Convert SHA1 Hash to String
  41.     shaHashString := hex.EncodeToString(shaHash.Sum(nil))
  42.  
  43.     // Creating a Regular Expression instance to find digits
  44.     // from the SHA1 hash
  45.     regEx := regexp.MustCompile("\\d")
  46.  
  47.     // Extracting 8 digits
  48.     digitsArr := regEx.FindAllString(shaHashString, -1)[0:8]
  49.  
  50.     return strconv.ParseInt(
  51.         strings.Join(digitsArr[0:8], ""), // Join array to form a string
  52.         10,                               // base 10 numeral
  53.         64)                               // Int64
  54. }
  55.  
  56. // Function launch simply launches a hardcoded command/application
  57. func launch() {
  58.     // Create a command instance to be executed.
  59.     cmd := exec.Command("app.dll")
  60.  
  61.     // Run the command.
  62.     err := cmd.Run()
  63.     if err != nil {
  64.         fmt.Println("An error occured!")
  65.         os.Exit(0)
  66.     }
  67. }
  68.  
  69. // Function main is the entry point of our application.
  70. func main() {
  71.     // Print the message to ask for OTP
  72.     fmt.Println("Enter OTP to launch")
  73.     fmt.Println("---------------------")
  74.  
  75.     var otp int64 // To store entered OTP
  76.  
  77.     _, err := fmt.Scan(&otp) // Enter the OTP
  78.  
  79.     // Check if we've got any errors.
  80.     if err != nil {
  81.         fmt.Println("An error occured!")
  82.         // Quit in case of any error.
  83.         os.Exit(0)
  84.     }
  85.  
  86.     // Find the correct OTP that we need to match
  87.     // the user input against.
  88.     realOTP, _ := getOTP()
  89.  
  90.     // Compare both real OTP and user's input
  91.     if otp == realOTP {
  92.         fmt.Println("Launching ...")
  93.         // Launch the application if OTP is correct.
  94.         launch()
  95.     } else {
  96.         fmt.Println("Wrong OTP!")
  97.     }
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top