Advertisement
Guest User

snap to luks

a guest
Oct 20th, 2023
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.15 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/binary"
  5.     "errors"
  6.     "fmt"
  7.     "os"
  8.     "strconv"
  9. )
  10.  
  11. // ParseRecoveryKey Parse[16]byte interprets the supplied string and returns the corresponding [16]byte. The recovery key is a
  12. // 16-byte number, and the formatted version of this is represented as 8 5-digit zero-extended base-10 numbers (each
  13. // with a range of 00000-65535) which may be separated by an optional '-', eg:
  14. //
  15. // "61665-00531-54469-09783-47273-19035-40077-28287"
  16. //
  17. // The formatted version of the recovery key is designed to be able to be inputted on a numeric keypad.
  18. func ParseRecoveryKey(s string) (out [16]byte, err error) {
  19.     for i := 0; i < 8; i++ {
  20.         if len(s) < 5 {
  21.             return [16]byte{}, errors.New("incorrectly formatted: insufficient characters")
  22.         }
  23.         x, err := strconv.ParseUint(s[0:5], 10, 16) // Base 10 16 bit int
  24.         if err != nil {
  25.             return [16]byte{}, errors.New("incorrectly formatted")
  26.         }
  27.         binary.LittleEndian.PutUint16(out[i*2:], uint16(x))
  28.  
  29.         // Move to the next 5 digits
  30.         s = s[5:]
  31.         // Permit each set of 5 digits to be separated by an optional '-', but don't allow the formatted key to end or begin with one.
  32.         if len(s) > 1 && s[0] == '-' {
  33.             s = s[1:]
  34.         }
  35.     }
  36.  
  37.     if len(s) > 0 {
  38.         return [16]byte{}, errors.New("incorrectly formatted: too many characters")
  39.     }
  40.  
  41.     return
  42. }
  43.  
  44. func selfTest() bool {
  45.     _, e := ParseRecoveryKey("61665-00531-54469-09783-47273-19035-40077-28287")
  46.  
  47.     if e != nil {
  48.         return false
  49.     } else {
  50.         return true
  51.     }
  52. }
  53.  
  54. func main() {
  55.     if !selfTest() {
  56.         fmt.Println("Self-test failed")
  57.         return
  58.     }
  59.  
  60.     fmt.Println("Please enter your Snap-encoded recovery key below:")
  61.     var recoveryKey string
  62.     _, err := fmt.Scanln(&recoveryKey)
  63.     if err != nil {
  64.         fmt.Println("Failed to read your key!")
  65.     }
  66.  
  67.     if key, e := ParseRecoveryKey(recoveryKey); e != nil {
  68.         fmt.Printf("Failed to decode recovery key; %s\n", e)
  69.     } else {
  70.         //fmt.Printf("Your recovery key is: ")
  71.         //_, _ = os.Stderr.Write(key[:])
  72.         //fmt.Println()
  73.         fmt.Println("Dumping to ./key.out for your convenience!")
  74.  
  75.         if err = os.WriteFile("key.out", key[:], 0600); err != nil {
  76.             fmt.Printf("Error writing to key.out: %s", err)
  77.         }
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement