87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
package utils
|
|
|
|
import "strings"
|
|
|
|
const (
|
|
PemPrivateKHeader = "-----BEGIN RSA PRIVATE KEY-----"
|
|
PemPrivateKFooter = "-----END RSA PRIVATE KEY-----"
|
|
)
|
|
|
|
const (
|
|
PemPublicHeader = "-----BEGIN PUBLIC KEY-----"
|
|
PemPublicPuFooter = "-----END PUBLIC KEY-----"
|
|
)
|
|
|
|
type Private struct {
|
|
pemHeader string
|
|
pemFooter string
|
|
}
|
|
|
|
type Public struct {
|
|
pemHeader string
|
|
pemFooter string
|
|
}
|
|
|
|
type OptionPri func(p *Private)
|
|
type OptionPub func(p *Public)
|
|
|
|
func WithPemPriHeader(pemHeader string) OptionPri {
|
|
return func(p *Private) {
|
|
p.pemHeader = pemHeader
|
|
}
|
|
}
|
|
func WithPemPriFooter(pemFooter string) OptionPri {
|
|
return func(p *Private) {
|
|
p.pemFooter = pemFooter
|
|
}
|
|
}
|
|
|
|
func NewPrivate(options ...OptionPri) *Private {
|
|
p := &Private{
|
|
pemHeader: PemPrivateKHeader,
|
|
pemFooter: PemPrivateKFooter,
|
|
}
|
|
for _, option := range options {
|
|
option(p)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (p *Private) Build(keyStr string) string {
|
|
return buildPem(p.pemHeader, p.pemFooter, keyStr)
|
|
}
|
|
|
|
func WithPemPukHeader(pemHeader string) OptionPub {
|
|
return func(p *Public) {
|
|
p.pemHeader = pemHeader
|
|
}
|
|
}
|
|
func WithPemPukFooter(pemFooter string) OptionPub {
|
|
return func(p *Public) {
|
|
p.pemFooter = pemFooter
|
|
}
|
|
}
|
|
|
|
func NewPublic(options ...OptionPub) *Public {
|
|
p := &Public{
|
|
pemHeader: PemPublicHeader,
|
|
pemFooter: PemPublicPuFooter,
|
|
}
|
|
for _, option := range options {
|
|
option(p)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (p *Public) Build(keyStr string) string {
|
|
return buildPem(p.pemHeader, p.pemFooter, keyStr)
|
|
}
|
|
|
|
func buildPem(pemHeader, pemFooter, keyStr string) string {
|
|
var lines []string
|
|
lines = append(lines, pemHeader)
|
|
lines = append(lines, keyStr)
|
|
lines = append(lines, pemFooter)
|
|
return strings.Join(lines, "\n")
|
|
}
|