github.com/annchain/OG@v0.0.9/common/encryption/encryption.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package encryption
    16  
    17  import (
    18  	"crypto/aes"
    19  	"crypto/cipher"
    20  	"crypto/md5"
    21  	"crypto/rand"
    22  	"encoding/hex"
    23  	"io"
    24  	"io/ioutil"
    25  	"os"
    26  )
    27  
    28  func createHash(key string) string {
    29  	hasher := md5.New()
    30  	hasher.Write([]byte(key))
    31  	return hex.EncodeToString(hasher.Sum(nil))
    32  }
    33  
    34  func encrypt(data []byte, passphrase string) (ciphertext []byte, err error) {
    35  	block, _ := aes.NewCipher([]byte(createHash(passphrase)))
    36  	gcm, err := cipher.NewGCM(block)
    37  	if err != nil {
    38  		return
    39  	}
    40  	nonce := make([]byte, gcm.NonceSize())
    41  	if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
    42  		return
    43  	}
    44  	ciphertext = gcm.Seal(nonce, nonce, data, nil)
    45  	return
    46  }
    47  
    48  func decrypt(data []byte, passphrase string) (plaintext []byte, err error) {
    49  	key := []byte(createHash(passphrase))
    50  	block, err := aes.NewCipher(key)
    51  	if err != nil {
    52  		return
    53  	}
    54  	gcm, err := cipher.NewGCM(block)
    55  	if err != nil {
    56  		return
    57  	}
    58  	nonceSize := gcm.NonceSize()
    59  	nonce, ciphertext := data[:nonceSize], data[nonceSize:]
    60  	plaintext, err = gcm.Open(nil, nonce, ciphertext, nil)
    61  	return
    62  }
    63  
    64  func EncryptFile(filename string, data []byte, passphrase string) (err error) {
    65  	f, _ := os.Create(filename)
    66  	defer f.Close()
    67  	cipherText, err := encrypt(data, passphrase)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	_, err = f.Write(cipherText)
    72  	return
    73  }
    74  
    75  func DecryptFile(filename string, passphrase string) (data []byte, err error) {
    76  	data, err = ioutil.ReadFile(filename)
    77  	if err != nil {
    78  		return
    79  	}
    80  	return decrypt(data, passphrase)
    81  }
    82  
    83  func EncryptFileDummy(filename string, data []byte, passphrase string) (err error) {
    84  	f, _ := os.Create(filename)
    85  	defer f.Close()
    86  	_, err = f.Write(data)
    87  	return
    88  }
    89  
    90  func DecryptFileDummy(filename string, passphrase string) (data []byte, err error) {
    91  	data, err = ioutil.ReadFile(filename)
    92  	return
    93  }