Encrypt encrypts the given data using crypter-defined key and returns a name that should be used to save the blob in the repository.
(c Crypter, payload gather.Bytes, prefix, suffix blob.ID, output *gather.WriteBuffer)
| 42 | // Encrypt encrypts the given data using crypter-defined key and returns a name that should |
| 43 | // be used to save the blob in the repository. |
| 44 | func Encrypt(c Crypter, payload gather.Bytes, prefix, suffix blob.ID, output *gather.WriteBuffer) (blob.ID, error) { |
| 45 | var hashOutput [hashing.MaxHashSize]byte |
| 46 | |
| 47 | hash := c.HashFunc()(hashOutput[:0], payload) |
| 48 | blobID := prefix + blob.ID(hex.EncodeToString(hash)) |
| 49 | |
| 50 | if suffix != "" { |
| 51 | blobID += "-" + suffix |
| 52 | } |
| 53 | |
| 54 | iv, err := getIndexBlobIV(blobID) |
| 55 | if err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | |
| 59 | output.Reset() |
| 60 | |
| 61 | if err := c.Encryptor().Encrypt(payload, iv, output); err != nil { |
| 62 | return "", errors.Wrapf(err, "error encrypting BLOB %v", blobID) |
| 63 | } |
| 64 | |
| 65 | return blobID, nil |
| 66 | } |
| 67 | |
| 68 | // Decrypt decrypts the provided data using provided blobID to derive initialization vector. |
| 69 | func Decrypt(c Crypter, payload gather.Bytes, blobID blob.ID, output *gather.WriteBuffer) error { |