File: armor.go

package info (click to toggle)
golang-github-hiddeco-sshsig 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 17
file content (31 lines) | stat: -rw-r--r-- 763 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package sshsig

import (
	"encoding/pem"
	"errors"
	"fmt"
)

// PEMType is the PEM type of an armored SSH signature.
const PEMType = "SSH SIGNATURE"

// Armor returns a PEM-encoded Signature. It does not perform any validation.
func Armor(s *Signature) []byte {
	return pem.EncodeToMemory(&pem.Block{
		Type:  PEMType,
		Bytes: s.Marshal(),
	})
}

// Unarmor decodes a PEM-encoded signature into a Signature. It returns an
// error if the PEM block is invalid or the signature parsing fails.
func Unarmor(b []byte) (*Signature, error) {
	p, _ := pem.Decode(b)
	if p == nil {
		return nil, errors.New("invalid PEM block")
	}
	if p.Type != PEMType {
		return nil, fmt.Errorf("invalid PEM type %q: expected %q", p.Type, PEMType)
	}
	return ParseSignature(p.Bytes)
}