From b01f065b251063bcab64bdab933e998ea4315ee1 Mon Sep 17 00:00:00 2001 From: Andrew Gallagher Date: Tue, 3 Dec 2024 10:45:30 +0000 Subject: [PATCH] Emit armor headers in deterministically sorted order (#255) --- openpgp/armor/encode.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/openpgp/armor/encode.go b/openpgp/armor/encode.go index 112f98b8..550efddf 100644 --- a/openpgp/armor/encode.go +++ b/openpgp/armor/encode.go @@ -7,6 +7,7 @@ package armor import ( "encoding/base64" "io" + "sort" ) var armorHeaderSep = []byte(": ") @@ -159,8 +160,15 @@ func encode(out io.Writer, blockType string, headers map[string]string, checksum return } - for k, v := range headers { - err = writeSlices(out, []byte(k), armorHeaderSep, []byte(v), newline) + keys := make([]string, len(headers)) + i := 0 + for k := range headers { + keys[i] = k + i++ + } + sort.Strings(keys) + for _, k := range keys { + err = writeSlices(out, []byte(k), armorHeaderSep, []byte(headers[k]), newline) if err != nil { return }