Skip to content

Commit 6d67b48

Browse files
kalexmillsmpvl
authored andcommitted
pkg/crypto/hmac: add crypto/hmac support
Adds interface for the standard library crypto/hmac package, along with a way to select from a list of supported hash functions. Fixes #788 Change-Id: Ifdbe031a6da4a8690448ae66d562079068539449 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8861 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 9d044f0 commit 6d67b48

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

pkg/crypto/hmac/hmac.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package hmac
15+
16+
import (
17+
"crypto/hmac"
18+
"crypto/md5"
19+
"crypto/sha1"
20+
"crypto/sha256"
21+
"crypto/sha512"
22+
"fmt"
23+
"hash"
24+
)
25+
26+
const (
27+
MD5 = "MD5"
28+
SHA1 = "SHA1"
29+
SHA224 = "SHA224"
30+
SHA256 = "SHA256"
31+
SHA384 = "SHA384"
32+
SHA512 = "SHA512"
33+
SHA512_224 = "SHA512_224"
34+
SHA512_256 = "SHA512_256"
35+
)
36+
37+
// Sign returns the HMAC signature of the data, using the provided key and hash function.
38+
//
39+
// Supported hash functions: "MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512", "SHA512_224",
40+
// and "SHA512_256".
41+
func Sign(hashName string, key []byte, data []byte) ([]byte, error) {
42+
hash, err := hashFromName(hashName)
43+
if err != nil {
44+
return nil, err
45+
}
46+
mac := hmac.New(hash, key)
47+
mac.Write(data)
48+
return mac.Sum(nil), nil
49+
}
50+
51+
func hashFromName(hash string) (func() hash.Hash, error) {
52+
switch hash {
53+
case MD5:
54+
return md5.New, nil
55+
case SHA1:
56+
return sha1.New, nil
57+
case SHA224:
58+
return sha256.New224, nil
59+
case SHA256:
60+
return sha256.New, nil
61+
case SHA384:
62+
return sha512.New384, nil
63+
case SHA512:
64+
return sha512.New, nil
65+
case SHA512_224:
66+
return sha512.New512_224, nil
67+
case SHA512_256:
68+
return sha512.New512_256, nil
69+
}
70+
return nil, fmt.Errorf("unsupported hash function")
71+
}

pkg/crypto/hmac/hmac_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package hmac_test
15+
16+
import (
17+
"testing"
18+
19+
"cuelang.org/go/pkg/internal/builtintest"
20+
)
21+
22+
func TestBuiltin(t *testing.T) {
23+
builtintest.Run("hmac", t)
24+
}

pkg/crypto/hmac/pkg.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crypto/hmac/testdata/hmac.txtar

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- in.cue --
2+
import "crypto/hmac"
3+
import "encoding/hex"
4+
5+
t1: hex.Encode(hmac.Sign(hmac.SHA1, hex.Decode("303132333435363738393a3b3c3d3e3f40414243"), "Sample #2"))
6+
t2: hex.Encode(hmac.Sign(hmac.MD5, "Jefe", "what do ya want for nothing?"))
7+
t3: hex.Encode(hmac.Sign(hmac.SHA256, hex.Decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), "Hi There"))
8+
t4: hex.Encode(hmac.Sign(hmac.SHA224, hex.Decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b"), "Sample message for keylen<blocklen"))
9+
t5: hex.Encode(hmac.Sign(hmac.SHA384, hex.Decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f"), "Sample message for keylen<blocklen"))
10+
-- out/hmac --
11+
t1: "0922d3405faa3d194f82a45830737d5cc6c75d24"
12+
t2: "750c783e6ab0b503eaa86e310a5db738"
13+
t3: "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
14+
t4: "e3d249a8cfb67ef8b7a169e9a0a599714a2cecba65999a51beb8fbbe"
15+
t5: "6eb242bdbb582ca17bebfa481b1e23211464d2b7f8c20b9ff2201637b93646af5ae9ac316e98db45d9cae773675eeed0"
16+

pkg/register.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package pkg
1616

1717
import (
18+
_ "cuelang.org/go/pkg/crypto/hmac"
1819
_ "cuelang.org/go/pkg/crypto/md5"
1920
_ "cuelang.org/go/pkg/crypto/sha1"
2021
_ "cuelang.org/go/pkg/crypto/sha256"

0 commit comments

Comments
 (0)