diff options
author | Wim <wim@42.be> | 2019-05-30 12:20:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-30 12:20:56 +0200 |
commit | 3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4 (patch) | |
tree | c5358b971a95749bece9469e959041d4f2e54cc3 /vendor/github.com/matterbridge/go-whatsapp/crypto/cbc/cbc.go | |
parent | 9619dff33417548a50e51a4f75f41b9de4a73327 (diff) | |
download | matterbridge-msglm-3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4.tar.gz matterbridge-msglm-3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4.tar.bz2 matterbridge-msglm-3418e8c9afbdf3e94ab26a20d8f12c042ae29fc4.zip |
Use upstream whatsapp again (#809)
Diffstat (limited to 'vendor/github.com/matterbridge/go-whatsapp/crypto/cbc/cbc.go')
-rw-r--r-- | vendor/github.com/matterbridge/go-whatsapp/crypto/cbc/cbc.go | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/vendor/github.com/matterbridge/go-whatsapp/crypto/cbc/cbc.go b/vendor/github.com/matterbridge/go-whatsapp/crypto/cbc/cbc.go deleted file mode 100644 index cc5427c3..00000000 --- a/vendor/github.com/matterbridge/go-whatsapp/crypto/cbc/cbc.go +++ /dev/null @@ -1,101 +0,0 @@ -/* -CBC describes a block cipher mode. In cryptography, a block cipher mode of operation is an algorithm that uses a -block cipher to provide an information service such as confidentiality or authenticity. A block cipher by itself -is only suitable for the secure cryptographic transformation (encryption or decryption) of one fixed-length group of -bits called a block. A mode of operation describes how to repeatedly apply a cipher's single-block operation to -securely transform amounts of data larger than a block. - -This package simplifies the usage of AES-256-CBC. -*/ -package cbc - -/* -Some code is provided by the GitHub user locked (github.com/locked): -https://gist.github.com/locked/b066aa1ddeb2b28e855e -Thanks! -*/ -import ( - "bytes" - "crypto/aes" - "crypto/cipher" - "crypto/rand" - "fmt" - "io" -) - -/* -Decrypt is a function that decrypts a given cipher text with a provided key and initialization vector(iv). -*/ -func Decrypt(key, iv, ciphertext []byte) ([]byte, error) { - block, err := aes.NewCipher(key) - - if err != nil { - return nil, err - } - - if len(ciphertext) < aes.BlockSize { - return nil, fmt.Errorf("ciphertext is shorter then block size: %d / %d", len(ciphertext), aes.BlockSize) - } - - if iv == nil { - iv = ciphertext[:aes.BlockSize] - ciphertext = ciphertext[aes.BlockSize:] - } - - cbc := cipher.NewCBCDecrypter(block, iv) - cbc.CryptBlocks(ciphertext, ciphertext) - - return unpad(ciphertext) -} - -/* -Encrypt is a function that encrypts plaintext with a given key and an optional initialization vector(iv). -*/ -func Encrypt(key, iv, plaintext []byte) ([]byte, error) { - plaintext = pad(plaintext, aes.BlockSize) - - if len(plaintext)%aes.BlockSize != 0 { - return nil, fmt.Errorf("plaintext is not a multiple of the block size: %d / %d", len(plaintext), aes.BlockSize) - } - - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - - var ciphertext []byte - if iv == nil { - ciphertext = make([]byte, aes.BlockSize+len(plaintext)) - iv := ciphertext[:aes.BlockSize] - if _, err := io.ReadFull(rand.Reader, iv); err != nil { - return nil, err - } - - cbc := cipher.NewCBCEncrypter(block, iv) - cbc.CryptBlocks(ciphertext[aes.BlockSize:], plaintext) - } else { - ciphertext = make([]byte, len(plaintext)) - - cbc := cipher.NewCBCEncrypter(block, iv) - cbc.CryptBlocks(ciphertext, plaintext) - } - - return ciphertext, nil -} - -func pad(ciphertext []byte, blockSize int) []byte { - padding := blockSize - len(ciphertext)%blockSize - padtext := bytes.Repeat([]byte{byte(padding)}, padding) - return append(ciphertext, padtext...) -} - -func unpad(src []byte) ([]byte, error) { - length := len(src) - padLen := int(src[length-1]) - - if padLen > length { - return nil, fmt.Errorf("padding is greater then the length: %d / %d", padLen, length) - } - - return src[:(length - padLen)], nil -} |