crypto.go raw

   1  package gcm
   2  
   3  import (
   4  	"crypto/aes"
   5  	"crypto/cipher"
   6  
   7  	"golang.org/x/crypto/argon2"
   8  )
   9  
  10  // GetCipher returns a GCM cipher given a password string. Note that this cipher must be renewed every 4gb of encrypted
  11  // data
  12  func GetCipher(password []byte) (gcm cipher.AEAD, e error) {
  13  	bytes := make([]byte, len(password))
  14  	rb := make([]byte, len(password))
  15  	copy(bytes, password)
  16  	copy(rb, password)
  17  	var c cipher.Block
  18  	rb = reverse(bytes)
  19  	ark := argon2.IDKey(rb, bytes, 1, 64*1024, 4, 32)
  20  	if c, e = aes.NewCipher(ark); E.Chk(e) {
  21  		return
  22  	}
  23  	if gcm, e = cipher.NewGCM(c); E.Chk(e) {
  24  	}
  25  	for i := range bytes {
  26  		bytes[i] = 0
  27  		rb[i] = 0
  28  	}
  29  	return
  30  }
  31  
  32  func reverse(b []byte) []byte {
  33  	for i := range b {
  34  		b[i], b[len(b)-1] = b[len(b)-1], b[i]
  35  	}
  36  	return b
  37  }
  38