Created
June 23, 2022 04:41
-
-
Save Eriner/076c77bf0359d928c8bdfd0841056947 to your computer and use it in GitHub Desktop.
a poor implementation w/ discarding the 5 most significant bits... will fix after I implement in JS first.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/md5" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"strings" | |
) | |
const ( | |
salt string = "abc" | |
myDomain string = "mydomain.example" | |
bipFile string = "bip39.txt" | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
log.Fatal("you must provide exactly one domain as argument") | |
} | |
domain := os.Args[1] | |
sum := md5.Sum([]byte(domain + "+" + salt)) | |
var nums []int | |
// read two bytes (16 bits) | |
for i := 0; i < len(sum)-1; i++ { | |
x := int(sum[i])<<8 | int(sum[i+1]) | |
// ... and save the least significant 11 bits | |
x &= 0x7ff | |
nums = append(nums, x) | |
} | |
bipnums := nums[0:3] | |
// for each number, map to wordlist numbers | |
var addr string | |
for _, i := range bipnums { | |
word, err := wordFromIndex(bipFile, i) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if len(addr) == 0 { | |
addr = word | |
} else { | |
addr = strings.Join([]string{addr, word}, ".") | |
} | |
} | |
log.Printf("%s@%s\n", addr, myDomain) | |
} | |
func wordFromIndex(file string, index int) (string, error) { | |
dat, err := ioutil.ReadFile(file) | |
if err != nil { | |
return "", err | |
} | |
words := bytes.Split(dat, []byte("\n")) | |
if index > len(words) { | |
return "", fmt.Errorf("index out of range") | |
} | |
return string(words[index]), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment