mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-12-24 06:18:40 +08:00
25 lines
555 B
Go
25 lines
555 B
Go
package crypto
|
|
|
|
import (
|
|
"math/big"
|
|
|
|
"golang.org/x/crypto/curve25519"
|
|
)
|
|
|
|
// endecrypt encrypts a 32-byte slice given the their public & our private private curve25519 keys via simple XOR with the shared secret
|
|
func Curve25519Crypt(privKey, pubKey Key, payloadBuf []byte) ([]byte, error) {
|
|
|
|
// Perform static-static ECDH
|
|
keyBuf, err := curve25519.X25519(privKey[:], pubKey[:])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var key, payload, enc big.Int
|
|
key.SetBytes(keyBuf)
|
|
payload.SetBytes(payloadBuf)
|
|
enc.Xor(&key, &payload)
|
|
|
|
return enc.Bytes(), nil
|
|
}
|