more cleanup, fixes

This commit is contained in:
Sanjana Rajan 2018-06-04 17:50:26 -07:00
parent c254bd5d44
commit 04ebe6d459
9 changed files with 33 additions and 53 deletions

View file

@ -8,24 +8,12 @@ import (
"golang.org/x/crypto/openpgp/armor"
)
// ...Armor Type
type ArmorType string
func (at ArmorType) string() string {
return string(at)
}
const (
pgpMessageType ArmorType = "PGP MESSAGE"
pgpPublicBlockType ArmorType = "PGP PUBLIC KEY BLOCK"
pgpPrivateBlockType ArmorType = "PGP PRIVATE KEY BLOCK"
pgpMessageType string = "PGP MESSAGE"
pgpPublicBlockType string = "PGP PUBLIC KEY BLOCK"
pgpPrivateBlockType string = "PGP PRIVATE KEY BLOCK"
)
// ArmorKey make bytes input key to armor format
func ArmorKey(input []byte) (string, error) {
return ArmorWithType(input, pgpPublicBlockType.string())
}
// ArmorWithType make bytes input to armor format
func ArmorWithType(input []byte, armorType string) (string, error) {
var b bytes.Buffer

View file

@ -14,7 +14,7 @@ import (
func (o *OpenPGP) EncryptAttachmentBinKey(plainData []byte, fileName string, publicKey []byte) (*EncryptedSplit, error) {
var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader)
w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil {
return nil, err
}
@ -35,12 +35,12 @@ func (o *OpenPGP) EncryptAttachmentBinKey(plainData []byte, fileName string, pub
ew.Close()
w.Close()
splited, err := SeparateKeyAndData(outBuf.String())
split, err := SeparateKeyAndData(outBuf.String())
if err != nil {
return nil, err
}
splited.Algo = "aes256"
return splited, nil
split.Algo = "aes256"
return split, nil
}
//EncryptAttachment ...
@ -109,7 +109,7 @@ func (o *OpenPGP) DecryptAttachment(keyPacket []byte, dataPacket []byte, private
func (o *OpenPGP) EncryptAttachmentWithPassword(plainData []byte, password string) (string, error) {
var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader)
w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil {
return "", err
}

View file

@ -1,7 +1,7 @@
package pm
var armorHeader = map[string]string{
"Version": "OpenPGP Mobile 0.0.1 (" + Version() + ")",
"Version": "OpenPGP Golang 0.0.1 (" + Version() + ")",
"Comment": "https://protonmail.com",
}

6
key.go
View file

@ -190,7 +190,7 @@ func (o *OpenPGP) GenerateKey(userName string, domain string, passphrase string,
return "", err
}
serialized := w.Bytes()
return ArmorWithType(serialized, pgpPrivateBlockType.string())
return ArmorWithType(serialized, pgpPrivateBlockType)
}
// UpdatePrivateKeyPassphrase ...
@ -235,7 +235,7 @@ func (o *OpenPGP) UpdatePrivateKeyPassphrase(privateKey string, oldPassphrase st
}
serialized := w.Bytes()
return ArmorWithType(serialized, pgpPrivateBlockType.string())
return ArmorWithType(serialized, pgpPrivateBlockType)
}
// PublicKey get a public key from a private key
@ -251,7 +251,7 @@ func PublicKey(privateKey string) (string, error) {
e.Serialize(&outBuf)
}
outString, err := ArmorKey(outBuf.Bytes())
outString, err := ArmorWithType(outBuf.Bytes(), pgpPublicBlockType)
if err != nil {
return "", nil
}

View file

@ -220,7 +220,7 @@ func (o *OpenPGP) EncryptMessage(plainText string, publicKey string, privateKey
func (o *OpenPGP) EncryptMessageBinKey(plainText string, publicKey []byte, privateKey string, passphrase string, trim bool) (string, error) {
var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader)
w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil {
return "", err
}
@ -272,7 +272,7 @@ func (o *OpenPGP) EncryptMessageBinKey(plainText string, publicKey []byte, priva
func (o *OpenPGP) EncryptMessageWithPassword(plainText string, password string) (string, error) {
var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader)
w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil {
return "", err
}

View file

@ -1,12 +1,11 @@
package pm
// OpenPGP strutature to manager mutiple address keys and user keys
// OpenPGP structure to manage mutiple address keys and user keys
type OpenPGP struct {
// key ring not in used
addresses []*Address
//lastestServerTime unix time cache
lastestServerTime int64
//latestServerTime unix time cache
latestServerTime int64
}
// //AddAddress add a new address to key ring

View file

@ -198,7 +198,7 @@ func GetSessionFromSymmetricPacket(keyPackage []byte, password string) (*Session
if err == nil {
return &SessionSplit{
Session: key,
Algo: getAlog(cipherFunc),
Algo: getAlgo(cipherFunc),
}, nil
}
@ -240,7 +240,7 @@ var symKeyAlgos = map[string]packet.CipherFunction{
"aes256": packet.CipherAES256,
}
// Get this's cipher function.
// Get cipher function.
func cipherFunc(algo string) packet.CipherFunction {
cf, ok := symKeyAlgos[algo]
if ok {
@ -253,16 +253,13 @@ func getSessionSplit(ek *packet.EncryptedKey) (*SessionSplit, error) {
if ek == nil {
return nil, errors.New("can't decrypt key packet")
}
var algo string
algo := "aes256"
for k, v := range symKeyAlgos {
if v == ek.CipherFunc {
algo = k
break
}
}
if algo == "" {
algo = "aes256"
}
return &SessionSplit{
Session: ek.Key,
@ -270,17 +267,14 @@ func getSessionSplit(ek *packet.EncryptedKey) (*SessionSplit, error) {
}, nil
}
func getAlog(cipher packet.CipherFunction) string {
var algo string
func getAlgo(cipher packet.CipherFunction) string {
algo := "aes256"
for k, v := range symKeyAlgos {
if v == cipher {
algo = k
break
}
}
if algo == "" {
algo = "aes256"
}
return algo
}
@ -318,7 +312,7 @@ func SeparateKeyAndData(encrypted string) (*EncryptedSplit, error) {
//kr *KeyRing, r io.Reader) (key *SymmetricKey, symEncryptedData []byte,
packets := packet.NewReader(encryptedReader)
outSplt := &EncryptedSplit{}
outSplit := &EncryptedSplit{}
// Save encrypted key and signature apart
var ek *packet.EncryptedKey
@ -350,15 +344,15 @@ func SeparateKeyAndData(encrypted string) (*EncryptedSplit, error) {
symEncryptedData = append(symEncryptedData, byte(1))
symEncryptedData = append(symEncryptedData, packetContents...)
outSplt.DataPacket = symEncryptedData
outSplit.DataPacket = symEncryptedData
break
}
}
var buff bytes.Buffer
ek.Serialize(&buff)
outSplt.KeyPacket = buff.Bytes()
var buf bytes.Buffer
ek.Serialize(&buf)
outSplit.KeyPacket = buf.Bytes()
return outSplt, err
return outSplit, err
}

View file

@ -45,7 +45,7 @@ func (o *OpenPGP) SignTextDetached(plainText string, privateKey string, passphra
}
if signEntity == nil {
return "", errors.New("cannot sign message, singer key is not unlocked")
return "", errors.New("cannot sign message, signer key is not unlocked")
}
config := &packet.Config{DefaultCipher: packet.CipherAES256}

View file

@ -6,19 +6,18 @@ import (
// UpdateTime update cached time
func (o *OpenPGP) UpdateTime(newTime int64) {
o.lastestServerTime = newTime
o.latestServerTime = newTime
}
//GetTime get latest cached time
func (o *OpenPGP) GetTime() int64 {
return o.lastestServerTime
return o.latestServerTime
}
func (o *OpenPGP) getNow() time.Time {
if o.lastestServerTime > 0 {
tm := time.Unix(o.lastestServerTime, 0)
return tm
if o.latestServerTime > 0 {
return time.Unix(o.latestServerTime, 0)
}
return time.Now()