forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
armor62_decrypt.go
58 lines (53 loc) · 1.6 KB
/
armor62_decrypt.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package kbcmf
import (
"bytes"
"io"
"io/ioutil"
)
// NewDearmor62DecryptStream makes a new stream that dearmors and decrypts the given
// Reader stream. Pass it a keyring so that it can lookup private and public keys
// as necessary
func NewDearmor62DecryptStream(ciphertext io.Reader, kr Keyring) (plaintext io.Reader, frame Frame, err error) {
dearmored, frame, err := NewArmor62DecoderStream(ciphertext)
if err != nil {
return nil, nil, err
}
r, err := NewDecryptStream(dearmored, kr)
if err != nil {
return nil, nil, err
}
return r, frame, nil
}
// Dearmor62DecryptOpen takes an armor62'ed, encrypted ciphertext and attempts to
// dearmor and decrypt it, using the provided keyring. Checks that the frames in the
// armor are as expected.
func Dearmor62DecryptOpen(ciphertext string, kr Keyring) (plaintext []byte, err error) {
buf := bytes.NewBufferString(ciphertext)
s, frame, err := NewDearmor62DecryptStream(buf, kr)
if err != nil {
return nil, err
}
out, err := ioutil.ReadAll(s)
if err != nil {
return nil, err
}
if err = CheckArmor62Frame(frame); err != nil {
return nil, err
}
return out, nil
}
func CheckArmor62Frame(frame Frame) error {
if hdr, err := frame.GetHeader(); err != nil {
return err
} else if hdr != EncryptionArmorHeader {
return ErrBadArmorHeader{EncryptionArmorHeader, hdr}
}
if ftr, err := frame.GetFooter(); err != nil {
return err
} else if ftr != EncryptionArmorFooter {
return ErrBadArmorFooter{EncryptionArmorFooter, ftr}
}
return nil
}