MCPcopy
hub / github.com/syncthing/syncthing / DecryptBytes

Function DecryptBytes

lib/protocol/encryption.go:497–516  ·  view source on GitHub ↗

DecryptBytes returns the decrypted bytes, or an error if decryption failed.

(data []byte, key *[keySize]byte)

Source from the content-addressed store, hash-verified

495// DecryptBytes returns the decrypted bytes, or an error if decryption
496// failed.
497func DecryptBytes(data []byte, key *[keySize]byte) ([]byte, error) {
498 if len(data) < blockOverhead {
499 return nil, errors.New("data too short")
500 }
501
502 aead, err := chacha20poly1305.NewX(key[:])
503 if err != nil {
504 // Can only fail if the key is the wrong length
505 panic("cipher failure: " + err.Error())
506 }
507
508 if aead.NonceSize() != nonceSize || aead.Overhead() != tagSize {
509 // We want these values to be constant for our type declarations so
510 // we don't use the values returned by the GCM, but we verify them
511 // here.
512 panic("crypto parameter mismatch")
513 }
514
515 return aead.Open(nil, data[:nonceSize], data[nonceSize:], nil)
516}
517
518// randomNonce is a normal, cryptographically random nonce
519func randomNonce() *[nonceSize]byte {

Callers 5

decryptFileMethod · 0.92
TestKeyDerivationFunction · 0.85
TestEnDecryptBytesFunction · 0.85
RequestMethod · 0.85
DecryptFileInfoFunction · 0.85

Calls 3

NewMethod · 0.65
ErrorMethod · 0.65
OpenMethod · 0.65

Tested by 2

TestKeyDerivationFunction · 0.68
TestEnDecryptBytesFunction · 0.68