Nostr (Notes and Other Stuff Transmitted by Relays) with Symmetric Key in GoNostr (Notes and Other Stuff Transmitted by Relays) uses clients and relays, and where users run clients, and where anyone can be a relay. In this case, we will use symmetric key encryption to encrypt a message. Overall, Nostr uses AES CBC mode, and can use a 128-bit or a 256-bit encryption key. To generate the encryption key, we will use PBKDF2 to convert a password into a symmetric key. |
Code
The following is some sample code:
package main import ( "fmt" "os" "crypto/sha256" "golang.org/x/crypto/pbkdf2" "github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip04" ) func main() { message :="Hello world!" passwd:="qwerty" argCount := len(os.Args[1:]) if (argCount>0) {message = string(os.Args[1])} if (argCount>1) {passwd = string(os.Args[2])} sk := nostr.GeneratePrivateKey() pk, _ := nostr.GetPublicKey(sk) nostr_priv, _ := nip19.EncodePrivateKey(sk) nostr_pub, _ := nip19.EncodePublicKey(pk) fmt.Printf("Private key:\t%v\n",sk) fmt.Printf("Public key:\t%v\n", pk) fmt.Printf("Private Nosstr encoded:\t%v\n",nostr_priv) fmt.Printf("Public Nosstr encoded\t%v\n",nostr_pub) //sharedSecret := make([]byte, 32) salt:=[]byte("000000000000") sharedSecret := pbkdf2.Key([]byte(passwd), salt, 10000, 32, sha256.New) ciphertext, err := nip04.Encrypt(message, sharedSecret) if err != nil { fmt.Printf("Encryption failed: %s", err.Error()) } plaintext, err := nip04.Decrypt(ciphertext, sharedSecret) if err != nil { fmt.Printf("Decryption failed: %s", err.Error()) } fmt.Printf("\n\nMessage:\t%v\n",message) fmt.Printf("Password:\t%s\n",passwd) fmt.Printf("\nKey:\t\t%x\n",sharedSecret) fmt.Printf("\nCiphertext:\t%v\n",ciphertext) fmt.Printf("Plaintext:\t%v\n",plaintext) ev := nostr.Event{ PubKey: pk, CreatedAt: nostr.Now(), Kind: nostr.KindTextNote, Tags: nil, Content: ciphertext, } ev.Sign(sk) fmt.Printf("\nEvent (After signing): %v\n",ev) }
And a sample run:
Private key: e34ce39f2aaa54262f6800bb70be709a0cc740881471cc7a38c89b36d58489b0 Public key: e3c4c1edf967aef2f9995330f57c0a66f6e4c4c91952e4e6abeea351306500a3 Private Nosstr encoded: nsec1udxw88e24f2zvtmgqzahp0nsngxvwsygz3cuc73cezdnd4vy3xcqqs0x0c Public Nosstr encoded npub1u0zvrm0ev7h097ve2vc02lq2vmmwf3xfr9fwfe4ta634zvr9qz3skwzf4h Message: Testing 123 Password: Qwerty123 Key: 3b011c268cbd6c947e64fb0e368dd03a3b8af60812447de0db1f2c5e717beb67 Ciphertext: 76Ks9T5lGMADsyHdaEJ9Gg==?iv=A2HacRfX2D6iISHh2NBHqA== Plaintext: Testing 123 Event (After signing): {"kind":1,"id":"023bba8c54e88b29dcab91cdcd9aa5e1a1727858830631f8b57a5560f3dcdf99","pubkey":"e3c4c1edf967aef2f9995330f57c0a66f6e4c4c91952e4e6abeea351306500a3","created_at":1714916248,"tags":[],"content":"76Ks9T5lGMADsyHdaEJ9Gg==?iv=A2HacRfX2D6iISHh2NBHqA==","sig":"76c13edf39ef754ef263f41c7779e7473d567e5b8477b967d2d9e3ce34440ed0da302e5832e539a93ba911490959af83654c67fb8fd5e2f882dda2f13c6ef441"}
We see that the IV is added with "?iv=":
76Ks9T5lGMADsyHdaEJ9Gg==?iv=A2HacRfX2D6iISHh2NBHqA==