Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/binary"
- "errors"
- "fmt"
- "os"
- "strconv"
- )
- // ParseRecoveryKey Parse[16]byte interprets the supplied string and returns the corresponding [16]byte. The recovery key is a
- // 16-byte number, and the formatted version of this is represented as 8 5-digit zero-extended base-10 numbers (each
- // with a range of 00000-65535) which may be separated by an optional '-', eg:
- //
- // "61665-00531-54469-09783-47273-19035-40077-28287"
- //
- // The formatted version of the recovery key is designed to be able to be inputted on a numeric keypad.
- func ParseRecoveryKey(s string) (out [16]byte, err error) {
- for i := 0; i < 8; i++ {
- if len(s) < 5 {
- return [16]byte{}, errors.New("incorrectly formatted: insufficient characters")
- }
- x, err := strconv.ParseUint(s[0:5], 10, 16) // Base 10 16 bit int
- if err != nil {
- return [16]byte{}, errors.New("incorrectly formatted")
- }
- binary.LittleEndian.PutUint16(out[i*2:], uint16(x))
- // Move to the next 5 digits
- s = s[5:]
- // 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.
- if len(s) > 1 && s[0] == '-' {
- s = s[1:]
- }
- }
- if len(s) > 0 {
- return [16]byte{}, errors.New("incorrectly formatted: too many characters")
- }
- return
- }
- func selfTest() bool {
- _, e := ParseRecoveryKey("61665-00531-54469-09783-47273-19035-40077-28287")
- if e != nil {
- return false
- } else {
- return true
- }
- }
- func main() {
- if !selfTest() {
- fmt.Println("Self-test failed")
- return
- }
- fmt.Println("Please enter your Snap-encoded recovery key below:")
- var recoveryKey string
- _, err := fmt.Scanln(&recoveryKey)
- if err != nil {
- fmt.Println("Failed to read your key!")
- }
- if key, e := ParseRecoveryKey(recoveryKey); e != nil {
- fmt.Printf("Failed to decode recovery key; %s\n", e)
- } else {
- //fmt.Printf("Your recovery key is: ")
- //_, _ = os.Stderr.Write(key[:])
- //fmt.Println()
- fmt.Println("Dumping to ./key.out for your convenience!")
- if err = os.WriteFile("key.out", key[:], 0600); err != nil {
- fmt.Printf("Error writing to key.out: %s", err)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement