4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-07-04 22:54:07 +00:00

Update dependencies (#2180)

* Update dependencies

* Fix whatsmeow API changes
This commit is contained in:
Wim
2024-08-27 19:04:05 +02:00
committed by GitHub
parent d16645c952
commit c4157a4d5b
589 changed files with 681707 additions and 198856 deletions

View File

@ -18,7 +18,10 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"io"
)
@ -99,3 +102,47 @@ func unpad(src []byte) ([]byte, error) {
return src[:(length - padLen)], nil
}
func EncryptStream(key, iv, macKey []byte, plaintext io.Reader, ciphertext io.Writer) ([]byte, []byte, uint64, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, nil, 0, fmt.Errorf("failed to create cipher: %w", err)
}
cbc := cipher.NewCBCEncrypter(block, iv)
plainHasher := sha256.New()
cipherHasher := sha256.New()
cipherMAC := hmac.New(sha256.New, macKey)
cipherMAC.Write(iv)
buf := make([]byte, 32*1024)
var size int
hasMore := true
for hasMore {
var n int
n, err = io.ReadFull(plaintext, buf)
plainHasher.Write(buf[:n])
size += n
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
padding := aes.BlockSize - size%aes.BlockSize
buf = append(buf[:n], bytes.Repeat([]byte{byte(padding)}, padding)...)
hasMore = false
} else if err != nil {
return nil, nil, 0, fmt.Errorf("failed to read file: %w", err)
}
cbc.CryptBlocks(buf, buf)
cipherMAC.Write(buf)
cipherHasher.Write(buf)
_, err = ciphertext.Write(buf)
if err != nil {
return nil, nil, 0, fmt.Errorf("failed to write file: %w", err)
}
}
mac := cipherMAC.Sum(nil)[:10]
cipherHasher.Write(mac)
_, err = ciphertext.Write(mac)
if err != nil {
return nil, nil, 0, fmt.Errorf("failed to write checksum to file: %w", err)
}
return plainHasher.Sum(nil), cipherHasher.Sum(nil), uint64(size), nil
}