@@ -3,6 +3,7 @@ package encrypt
33import (
44 "bufio"
55 b64 "encoding/base64"
6+ "log"
67 "os"
78)
89
@@ -13,7 +14,6 @@ func Encrypt(publicKeyPath string, output string, data []byte) (encryptedKey, en
1314
1415 // AEs key
1516 key := NewAesEncryptionKey ()
16- // fmt.Printf("AES key: %v \n", *key)
1717 text := []byte (string (data ))
1818 encrypted , _ := AesEncrypt (text , key )
1919
@@ -26,14 +26,24 @@ func Encrypt(publicKeyPath string, output string, data []byte) (encryptedKey, en
2626}
2727
2828func Decrypt (privateKeyPath string , output string ) (string , error ) {
29- // Decrypt
3029 file , err := os .Open (output )
31-
3230 if err != nil {
3331 return "" , err
3432 }
33+ fi , err := file .Stat ()
34+ if err != nil {
35+ log .Fatal (err )
36+ }
3537
3638 scanner := bufio .NewScanner (file )
39+
40+ // We don't know the length of the longesst line in the file yet.
41+ // Let's just find the total size of the file and set that as
42+ // the maximum size of the buffer.
43+ var maxCapacity int = int (fi .Size ())
44+ buf := make ([]byte , maxCapacity )
45+ scanner .Buffer (buf , maxCapacity )
46+
3747 scanner .Split (bufio .ScanLines )
3848 var txtlines []string
3949
@@ -43,13 +53,25 @@ func Decrypt(privateKeyPath string, output string) (string, error) {
4353
4454 file .Close ()
4555
46- decryptData , _ := b64 .StdEncoding .DecodeString (txtlines [0 ])
47- decryptKey , _ := b64 .StdEncoding .DecodeString (txtlines [1 ])
56+ decryptData , err := b64 .StdEncoding .DecodeString (txtlines [0 ])
57+ if err != nil {
58+ return "" , err
59+ }
60+ decryptKey , err := b64 .StdEncoding .DecodeString (txtlines [1 ])
61+ if err != nil {
62+ return "" , err
63+ }
4864
4965 // Decrypt rsa
5066 privateKey := ReadRsaPrivateKey (privateKeyPath )
51- decryptedKey , _ := RsaDecrypt (string (decryptKey ), privateKey )
52- decrypted , _ := AesDecrypt (decryptData , decryptedKey )
67+ decryptedKey , err := RsaDecrypt (string (decryptKey ), privateKey )
68+ if err != nil {
69+ return "" , err
70+ }
71+ decrypted , err := AesDecrypt (decryptData , decryptedKey )
72+ if err != nil {
73+ return "" , err
74+ }
5375
5476 return string (decrypted ), nil
5577}
0 commit comments