github.com/blend/go-sdk@v1.20220411.3/crypto/encrypt.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package crypto 9 10 import ( 11 "crypto/aes" 12 "crypto/cipher" 13 cryptorand "crypto/rand" 14 "io" 15 ) 16 17 // Encrypt encrypts data with the given key. 18 func Encrypt(key, plainText []byte) ([]byte, error) { 19 block, err := aes.NewCipher(key) 20 if err != nil { 21 return nil, err 22 } 23 ciphertext := make([]byte, aes.BlockSize+len(plainText)) 24 iv := ciphertext[:aes.BlockSize] 25 if _, err := io.ReadFull(cryptorand.Reader, iv); err != nil { 26 return nil, err 27 } 28 cfb := cipher.NewCFBEncrypter(block, iv) 29 cfb.XORKeyStream(ciphertext[aes.BlockSize:], plainText) 30 return ciphertext, nil 31 }