diff --git a/armor.go b/armor.go index d7814e5..8e744e0 100644 --- a/armor.go +++ b/armor.go @@ -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 diff --git a/attachment.go b/attachment.go index 2d426c8..f26d510 100644 --- a/attachment.go +++ b/attachment.go @@ -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 } diff --git a/common.go b/common.go index 25758f3..17fa6ae 100644 --- a/common.go +++ b/common.go @@ -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", } diff --git a/key.go b/key.go index e860d20..7ee1aa0 100644 --- a/key.go +++ b/key.go @@ -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 } diff --git a/message.go b/message.go index fcadb15..2d7bf89 100644 --- a/message.go +++ b/message.go @@ -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 } diff --git a/openpgp.go b/openpgp.go index 7502659..d9619ef 100644 --- a/openpgp.go +++ b/openpgp.go @@ -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 diff --git a/session.go b/session.go index 846922d..1fb765f 100644 --- a/session.go +++ b/session.go @@ -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 } diff --git a/sign_detached.go b/sign_detached.go index 57f37d3..a65e969 100644 --- a/sign_detached.go +++ b/sign_detached.go @@ -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} diff --git a/time.go b/time.go index 17dd0af..61e0a28 100644 --- a/time.go +++ b/time.go @@ -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()