github.com/blend/go-sdk@v1.20220411.3/crypto/decrypt.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  
    14  	"github.com/blend/go-sdk/ex"
    15  )
    16  
    17  // Decrypt decrypts data with the given key.
    18  func Decrypt(key, cipherText []byte) ([]byte, error) {
    19  	if len(cipherText) < aes.BlockSize {
    20  		return nil, ex.New("cannot decrypt string: `cipherText` is smaller than AES block size", ex.OptMessagef("block size: %v", aes.BlockSize))
    21  	}
    22  
    23  	iv := cipherText[:aes.BlockSize]
    24  	cipherText = cipherText[aes.BlockSize:]
    25  
    26  	block, err := aes.NewCipher(key)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	cfb := cipher.NewCFBDecrypter(block, iv)
    32  	cfb.XORKeyStream(cipherText, cipherText)
    33  	return cipherText, nil
    34  }