From f1f96c25e062c56098f7b91a378e5858af7dbd34 Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Sun, 6 Aug 2023 18:36:06 +0200 Subject: [PATCH] plugin: build tag EncodeX25519Recipient which uses crypto/ecdh --- plugin/encode.go | 11 ----------- plugin/encode_go1.20.go | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 plugin/encode_go1.20.go diff --git a/plugin/encode.go b/plugin/encode.go index 44912e5e..5000708a 100644 --- a/plugin/encode.go +++ b/plugin/encode.go @@ -5,7 +5,6 @@ package plugin import ( - "crypto/ecdh" "fmt" "strings" @@ -54,13 +53,3 @@ func ParseRecipient(s string) (name string, data []byte, err error) { name = strings.TrimPrefix(hrp, "age1") return name, data, nil } - -// EncodeX25519Recipient encodes a native X25519 recipient from a -// [crypto/ecdh.X25519] public key. It's meant for plugins that implement -// identities that are compatible with native recipients. -func EncodeX25519Recipient(pk *ecdh.PublicKey) (string, error) { - if pk.Curve() != ecdh.X25519() { - return "", fmt.Errorf("wrong ecdh Curve") - } - return bech32.Encode("age", pk.Bytes()) -} diff --git a/plugin/encode_go1.20.go b/plugin/encode_go1.20.go new file mode 100644 index 00000000..6b171660 --- /dev/null +++ b/plugin/encode_go1.20.go @@ -0,0 +1,24 @@ +// Copyright 2023 The age Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build go1.20 + +package plugin + +import ( + "crypto/ecdh" + "fmt" + + "filippo.io/age/internal/bech32" +) + +// EncodeX25519Recipient encodes a native X25519 recipient from a +// [crypto/ecdh.X25519] public key. It's meant for plugins that implement +// identities that are compatible with native recipients. +func EncodeX25519Recipient(pk *ecdh.PublicKey) (string, error) { + if pk.Curve() != ecdh.X25519() { + return "", fmt.Errorf("wrong ecdh Curve") + } + return bech32.Encode("age", pk.Bytes()) +}