summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go
diff options
context:
space:
mode:
authorKrzysiek Madejski <krzysztof.madejski@epf.org.pl>2019-02-21 20:28:13 +0100
committerWim <wim@42.be>2019-02-21 20:28:13 +0100
commit55e79063d6edbbf4560fd14edc45ce9558afaf7a (patch)
tree9ee9470119066556210a9226ae05b2c73ebda5e6 /vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go
parent46f4bbb3b5e93ff489c0125c66b1c29fcb001e22 (diff)
downloadmatterbridge-msglm-55e79063d6edbbf4560fd14edc45ce9558afaf7a.tar.gz
matterbridge-msglm-55e79063d6edbbf4560fd14edc45ce9558afaf7a.tar.bz2
matterbridge-msglm-55e79063d6edbbf4560fd14edc45ce9558afaf7a.zip
Add initial WhatsApp support (#711)
Diffstat (limited to 'vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go')
-rw-r--r--vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go b/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go
new file mode 100644
index 00000000..e0be0587
--- /dev/null
+++ b/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go
@@ -0,0 +1,52 @@
+/*
+HKDF is a simple key derivation function (KDF) based on
+a hash-based message authentication code (HMAC). It was initially proposed by its authors as a building block in
+various protocols and applications, as well as to discourage the proliferation of multiple KDF mechanisms.
+The main approach HKDF follows is the "extract-then-expand" paradigm, where the KDF logically consists of two modules:
+the first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key, and then the
+second stage "expands" this key into several additional pseudorandom keys (the output of the KDF).
+*/
+package hkdf
+
+import (
+ "crypto/hmac"
+ "crypto/sha256"
+ "fmt"
+ "golang.org/x/crypto/hkdf"
+ "io"
+)
+
+/*
+Expand expands a given key with the HKDF algorithm.
+*/
+func Expand(key []byte, length int, info string) ([]byte, error) {
+ if info == "" {
+ keyBlock := hmac.New(sha256.New, key)
+ var out, last []byte
+
+ var blockIndex byte = 1
+ for i := 0; len(out) < length; i++ {
+ keyBlock.Reset()
+ //keyBlock.Write(append(append(last, []byte(info)...), blockIndex))
+ keyBlock.Write(last)
+ keyBlock.Write([]byte(info))
+ keyBlock.Write([]byte{blockIndex})
+ last = keyBlock.Sum(nil)
+ blockIndex += 1
+ out = append(out, last...)
+ }
+ return out[:length], nil
+ } else {
+ h := hkdf.New(sha256.New, key, nil, []byte(info))
+ out := make([]byte, length)
+ n, err := io.ReadAtLeast(h, out, length)
+ if err != nil {
+ return nil, err
+ }
+ if n != length {
+ return nil, fmt.Errorf("new key to short")
+ }
+
+ return out[:length], nil
+ }
+}