MCPcopy
hub / github.com/kopia/kopia / verifyCiphertextSamples

Function verifyCiphertextSamples

repo/encryption/encryption_test.go:133–171  ·  view source on GitHub ↗
(t *testing.T, masterKey, contentID, payload []byte, samples map[string]string)

Source from the content-addressed store, hash-verified

131}
132
133func verifyCiphertextSamples(t *testing.T, masterKey, contentID, payload []byte, samples map[string]string) {
134 t.Helper()
135
136 for _, encryptionAlgo := range encryption.SupportedAlgorithms(true) {
137 enc, err := encryption.CreateEncryptor(parameters{encryptionAlgo, masterKey})
138 if err != nil {
139 t.Fatal(err)
140 }
141
142 ct := samples[encryptionAlgo]
143 if ct == "" {
144 func() {
145 var v gather.WriteBuffer
146 defer v.Close()
147
148 require.NoError(t, enc.Encrypt(gather.FromSlice(payload), contentID, &v))
149
150 t.Errorf("missing ciphertext sample for %q: %q,", encryptionAlgo, hex.EncodeToString(payload))
151 }()
152 } else {
153 b, err := hex.DecodeString(ct)
154 if err != nil {
155 t.Errorf("invalid ciphertext for %v: %v", encryptionAlgo, err)
156 continue
157 }
158
159 func() {
160 var plainText gather.WriteBuffer
161 defer plainText.Close()
162
163 require.NoError(t, enc.Decrypt(gather.FromSlice(b), contentID, &plainText))
164
165 if v := plainText.ToByteSlice(); !bytes.Equal(v, payload) {
166 t.Errorf("invalid plaintext after decryption %x, want %x", v, payload)
167 }
168 }()
169 }
170 }
171}
172
173func BenchmarkEncryption(b *testing.B) {
174 masterKey := make([]byte, 32)

Callers 1

TestCiphertextSamplesFunction · 0.85

Calls 10

CloseMethod · 0.95
ToByteSliceMethod · 0.95
SupportedAlgorithmsFunction · 0.92
CreateEncryptorFunction · 0.92
FromSliceFunction · 0.92
HelperMethod · 0.80
ErrorfMethod · 0.80
EqualMethod · 0.80
EncryptMethod · 0.65
DecryptMethod · 0.65

Tested by

no test coverage detected