diff --git a/component/age/age.go b/component/age/age.go index 257f6789d..c16ec5108 100644 --- a/component/age/age.go +++ b/component/age/age.go @@ -68,6 +68,18 @@ func EncryptBytes(data []byte, recipients ...Recipient) ([]byte, error) { return buf.Bytes(), nil } +// ConvertToRecipient convert age.Identity to age.Recipient +func ConvertToRecipient(identity Identity) (Recipient, error) { + switch identity := identity.(type) { + case *age.X25519Identity: + return identity.Recipient(), nil + case *age.HybridIdentity: + return identity.Recipient(), nil + default: + return nil, fmt.Errorf("unexpected identity type: %T", identity) + } +} + func GenX25519KeyPair() (string, string, error) { identity, err := age.GenerateX25519Identity() if err != nil { @@ -86,7 +98,7 @@ func GenHybridKeyPair() (string, string, error) { func Main(args []string) { if len(args) < 1 { - panic("Using: age keygen/keygen-pq/decrypt/encrypt") + panic("Using: age keygen/keygen-pq/convert/decrypt/encrypt") } switch args[0] { case "keygen": @@ -105,6 +117,24 @@ func Main(args []string) { fmt.Printf("# created: %s\n", time.Now().Format(time.RFC3339)) fmt.Printf("# public key: %s\n", publicKey) fmt.Printf("%s\n", secretKey) + case "convert": + if len(args) < 1 { + panic("Using: age convert ") + } + identities, err := ParseIdentities(args[1]) + if err != nil { + panic(err) + } + if len(identities) == 0 { + panic("no identities found in the input") + } + for _, identity := range identities { + recipient, err := ConvertToRecipient(identity) + if err != nil { + panic(err) + } + fmt.Println(recipient) + } case "decrypt": if len(args) < 3 { panic("Using: age decrypt ") diff --git a/component/age/age_test.go b/component/age/age_test.go index 28ae7dcc6..54ece94a6 100644 --- a/component/age/age_test.go +++ b/component/age/age_test.go @@ -1,6 +1,7 @@ package age_test import ( + "fmt" "testing" "github.com/metacubex/mihomo/component/age" @@ -30,6 +31,18 @@ func TestAge(t *testing.T) { if err != nil { t.Fatal(err) } + if len(identities) != len(recipients) { + t.Fatal("identities and recipients are not equal") + } + for i, identity := range identities { + recipient, err := age.ConvertToRecipient(identity) + if err != nil { + t.Fatal(err) + } + if fmt.Sprint(recipient) != fmt.Sprint(recipients[i]) { + t.Fatal("recipient is not equal to recipients: ", recipient, " != ", recipients[i], "") + } + } rawData := []byte("hello world") encryptData, err := age.EncryptBytes(rawData, recipients...) if err != nil { diff --git a/docs/config.yaml b/docs/config.yaml index 8dfc3cba9..93e898bc9 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -1478,6 +1478,7 @@ proxy-providers: # # 实用工具: # # 您可以通过 "mihomo age keygen" 生成符合要求的 x25519 key # # 您可以通过 "mihomo age keygen-pq" 生成符合要求的 mlkem768-x25519 key + # # 您可以通过 "mihomo age convert " 从 age-secret-key 导出 age-public-key # # 您可以通过 "mihome age decrypt " 将已加密文件解密, 为 - 时会从标准输入读取, 为 - 时会往标准输出写入 # # 您可以通过 "mihome age encrypt " 将未加密文件加密, 为 - 时会从标准输入读取, 为 - 时会往标准输出写入 # # 参考实现: