• cow@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    6 hours ago

    Ok I played with this, made a very trivial encoding with 16 words in python for fun today.

    code
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives.asymmetric import ed25519
    import itertools
    import sys
    
    if sys.argv[1] == "words":
        with open("wordlist-65536.txt", "r") as f:
            words = [x.strip().lower() for x in f.readlines()]
    
        assert len(words) == 65536
    
        k = serialization.load_ssh_private_key(open(sys.argv[2], "rb").read(),
                                               password=None)
    
        j = k.private_bytes(encoding=serialization.Encoding.Raw,
                            format=serialization.PrivateFormat.Raw,
                            encryption_algorithm=serialization.NoEncryption())
    
        for a, b in itertools.batched(j, n=2):
            z = a << 8
            if b is not None:
                z = z | b
    
            print(words[z], end=" ")
    
        print()
    
    elif sys.argv[1] == "key":
        seed = [x.strip() for x in input("Enter your mnemonic ssh key: ").split()]
        assert len(seed) == 16
    
        with open("wordlist-65536.txt", "r") as f:
            words = [x.strip().lower() for x in f.readlines()]
    
        assert len(words) == 65536
    
        words_map = {}
        for i, word in enumerate(words):
            words_map[word] = i
    
        k = bytearray()
        for word in seed:
            z = words_map[word]
    
            b = z & 0xff
            a = (z >> 8) & 0xff
    
            k.append(a)
            k.append(b)
    
        k2 = ed25519.Ed25519PrivateKey.from_private_bytes(k)
    
        private_bytes = k2.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.OpenSSH,
            encryption_algorithm=serialization.NoEncryption())
    
        open(sys.argv[2], "wb+").write(private_bytes)
    ```:::