Advertisement
S41F

Untitled

Nov 11th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.69 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/hex"
  5.     "fmt"
  6.     "math/rand"
  7.     "regexp"
  8.     "strings"
  9.     "time"
  10. )
  11.  
  12. /**
  13.  * important computation, part 1 of 2
  14.  */
  15. func compute1(done chan bool) {
  16.     fmt.Printf("\n\n\n\n\n\n")
  17.     frames := []string{
  18.         "\033[6A\r               X \n                  \n               O  \n             Y/|\\Z\n               |\n              / \\\n",
  19.         "\033[6A\r                 \n             X    \n             Y_O  \n               |\\Z\n               |\n              / \\\n",
  20.         "\033[6A\r                 \n             XY   \n              (O  \n               |\\Z\n               |\n              / \\\n",
  21.         "\033[6A\r              Y  \n                  \n             X_O  \n               |\\Z\n               |\n              / \\\n",
  22.         "\033[6A\r               Y \n                  \n               O  \n             X/|\\Z\n               |\n              / \\\n",
  23.         "\033[6A\r                 \n                 Y\n               O_Z\n             X/|  \n               |\n              / \\\n",
  24.         "\033[6A\r                 \n                ZY\n               O) \n             X/|  \n               |\n              / \\\n",
  25.         "\033[6A\r                Z\n                  \n               O_Y\n             X/|  \n               |\n              / \\\n",
  26.     }
  27.     ctr := 0
  28.     for {
  29.         select {
  30.         case <-done:
  31.             return
  32.         case <-time.Tick(time.Duration(250) * time.Millisecond):
  33.             ctr++
  34.             s := frames[ctr%len(frames)]
  35.             x := []byte("\033[32mo\033[39m")
  36.             y := []byte("\033[34mo\033[39m")
  37.             z := []byte("\033[35mo\033[39m")
  38.             for t := 0; t < ctr/len(frames)%3; t++ {
  39.                 x = xor_slice(xor_slice(x, y), z)
  40.                 y = xor_slice(xor_slice(x, y), z)
  41.                 z = xor_slice(xor_slice(x, y), z)
  42.                 x = xor_slice(xor_slice(x, y), z)
  43.             }
  44.             s = strings.Replace(s, "X", string(x), 1)
  45.             s = strings.Replace(s, "Y", string(y), 1)
  46.             s = strings.Replace(s, "Z", string(z), 1)
  47.             fmt.Print(s)
  48.         }
  49.     }
  50. }
  51.  
  52. const Output = 16
  53. const Space = 100000
  54. const Rounds = 100000
  55.  
  56. /**
  57.  * important computation, part 2 of 2
  58.  *
  59.  * pro-tip: you should always roll your own crypto. This prevents the NSA or other attackers from using
  60.  * off-the-shelf tools to defeat your system.
  61.  */
  62. func compute2(data []byte, done chan bool) chan string {
  63.     r := make(chan string)
  64.  
  65.     go func() {
  66.         state := make([]int, Space)
  67.         j := 0
  68.         i := 0
  69.         for i = range state {
  70.             state[i] = i
  71.         }
  72.  
  73.         for t := 0; t < Space*Rounds; t++ {
  74.             i = (i + 1) % Space
  75.             j = (j + state[i] + int(data[i%len(data)])) % Space
  76.             state[i], state[j] = state[j], state[i]
  77.         }
  78.  
  79.         o := make([]byte, Output)
  80.         for t := 0; t < Output; t++ {
  81.             i = (i + 1) % Space
  82.             j = state[(state[i]+state[j])%Space]
  83.             o[t] = byte(j & 0xff)
  84.         }
  85.  
  86.         done <- true
  87.         r <- hex.EncodeToString(o)
  88.     }()
  89.  
  90.     return r
  91. }
  92.  
  93. /**
  94.  * Some other computation.
  95.  */
  96. func compute3(r chan byte) chan byte {
  97.     s := make(chan byte)
  98.     go func() {
  99.         var prev byte = 0
  100.         for v := range r {
  101.             if v != prev {
  102.                 s <- v
  103.                 prev = v
  104.             }
  105.         }
  106.         close(s)
  107.     }()
  108.     return s
  109. }
  110.  
  111. /**
  112.  * These aren't helpful, right?
  113.  */
  114. func compute4(input string) string {
  115.     rand.Seed(42)
  116.     m := make(map[int]int)
  117.     for len(m) < 26 {
  118.         c := rand.Int()%26 + 'a'
  119.         if _, ok := m[c]; !ok {
  120.             m[c] = len(m) + 'a'
  121.         }
  122.     }
  123.     r := ""
  124.     for _, c := range input {
  125.         r = fmt.Sprintf("%s%c", r, m[int(c)])
  126.     }
  127.     return r
  128. }
  129.  
  130. /**
  131.  * A boring helper function
  132.  */
  133. func xor_slice(a []byte, b []byte) []byte {
  134.     r := make([]byte, len(a))
  135.     for i, v := range a {
  136.         r[i] = v ^ b[i]
  137.     }
  138.     return r
  139. }
  140.  
  141. /**
  142.  * Another boring function
  143.  */
  144. func panicIfInvalid(s string) {
  145.     if !regexp.MustCompile("^[a-zA-Z0-9]{26}$").MatchString(s) {
  146.         panic("invalid input!\n\n")
  147.     }
  148. }
  149.  
  150. /**
  151.  * Last one
  152.  */
  153. func another_helper(input string) (r bool) {
  154.     r = true
  155.     for i, v := range input {
  156.         for j, w := range input {
  157.             r = r && (i > j || v <= w)
  158.       fmt.Println(r)
  159.         }
  160.     fmt.Println("\n\n")
  161.     }
  162.     return
  163. }
  164.  
  165. /**
  166.  * Pro-tip: start here.
  167.  */
  168. func main() {
  169.     input := "qwertyuiopasdfghjklzxcvbnm"
  170.     panicIfInvalid(input)
  171.  
  172.     done := make(chan bool)
  173.   //go compute1(done)
  174.   fmt.Println("1")
  175.   h := compute2([]byte(input), done)
  176.   fmt.Println(h)
  177.     s := make(chan byte, len(input))
  178.   fmt.Println(s)
  179.     r := compute3(s)
  180.   fmt.Println(r)
  181.     for _, c := range input {
  182.         s <- byte(c)
  183.     }
  184.     close(s)
  185.   fmt.Println("5")
  186.     input = ""
  187.     for c := range r {
  188.         input = fmt.Sprintf("%s%c", input, c)
  189.     }
  190.     panicIfInvalid(input)
  191.   fmt.Println(input)
  192.     input = compute4(input)
  193.   fmt.Println(input)
  194.     //flag := <-h
  195.   fmt.Println("7")
  196.     if !another_helper(input) {
  197.         panic("invalid input!")
  198.     }
  199.     panicIfInvalid(input)
  200.   fmt.Println("8")
  201.     //fmt.Printf("Congrats! 🚩  = flag-%s\n", flag)
  202.   return
  203. }
  204.  
  205. // author: Alok
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement