mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
added nip04.go docstrings
changed variable names in ComputeSharedSecret to emphasize that it is agnostic about who is sender and who is receiver.
This commit is contained in:
parent
67813257df
commit
ebe3d61484
@ -13,16 +13,18 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
)
|
||||
|
||||
// ECDH
|
||||
func ComputeSharedSecret(senderPrivKey string, receiverPubKey string) (sharedSecret []byte, err error) {
|
||||
privKeyBytes, err := hex.DecodeString(senderPrivKey)
|
||||
// ComputeSharedSecret returns a shared secret key used to encrypt messages.
|
||||
// The private and public keys should be hex encoded.
|
||||
// Uses the Diffie-Hellman key exchange (ECDH) (RFC 4753).
|
||||
func ComputeSharedSecret(pub string, sk string) (sharedSecret []byte, err error) {
|
||||
privKeyBytes, err := hex.DecodeString(sk)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error decoding sender private key: %s. \n", err)
|
||||
}
|
||||
privKey, _ := btcec.PrivKeyFromBytes(privKeyBytes)
|
||||
|
||||
// adding 02 to signal that this is a compressed public key (33 bytes)
|
||||
pubKeyBytes, err := hex.DecodeString("02" + receiverPubKey)
|
||||
pubKeyBytes, err := hex.DecodeString("02" + pub)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error decoding hex string of receiver public key: %s. \n", err)
|
||||
}
|
||||
@ -34,7 +36,9 @@ func ComputeSharedSecret(senderPrivKey string, receiverPubKey string) (sharedSec
|
||||
return btcec.GenerateSharedSecret(privKey, pubKey), nil
|
||||
}
|
||||
|
||||
// aes-256-cbc
|
||||
// Encrypt encrypts message with key using aes-256-cbc.
|
||||
// key should be the shared secret generated by ComputeSharedSecret.
|
||||
// Returns: base64(encrypted_bytes) + "?iv=" + base64(initialization_vector).
|
||||
func Encrypt(message string, key []byte) (string, error) {
|
||||
// block size is 16 bytes
|
||||
iv := make([]byte, 16)
|
||||
@ -70,7 +74,8 @@ func Encrypt(message string, key []byte) (string, error) {
|
||||
return base64.StdEncoding.EncodeToString(ciphertext) + "?iv=" + base64.StdEncoding.EncodeToString(iv), nil
|
||||
}
|
||||
|
||||
// aes-256-cbc
|
||||
// Decrypt decrypts a content string using the shared secret key.
|
||||
// The inverse operation to message -> Encrypt(message, key).
|
||||
func Decrypt(content string, key []byte) (string, error) {
|
||||
parts := strings.Split(content, "?iv=")
|
||||
if len(parts) < 2 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user