cue/pkg/crypto: make return types bytes, instead of list

Note that the generated builtins indicate a bytes
or string return type. This is fine, as the actual
conversion will restrict it to bytes.

Closes #92

Change-Id: Ib8a92d9feebf6f298dc0c35d2831f80e5d7a76ca
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3048
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cue/builtin_test.go b/cue/builtin_test.go
index 5c03f11..573da3f 100644
--- a/cue/builtin_test.go
+++ b/cue/builtin_test.go
@@ -57,6 +57,33 @@
 		test("math", `math.Floor("foo")`),
 		`_|_(cannot use "foo" (type string) as number in argument 1 to math.Floor)`,
 	}, {
+		test("crypto/sha256", `sha256.Sum256("hash me")`),
+		`'\xeb \x1a\xf5\xaa\xf0\xd6\x06)\xd3Ҧ\x1eFl\xfc\x0f\xed\xb5\x17\xad\xd81\xec\xacR5\xe1کc\xd6'`,
+	}, {
+		test("crypto/md5", `len(md5.Sum("hash me"))`),
+		`16`,
+	}, {
+		test("crypto/sha1", `len(sha1.Sum("hash me"))`),
+		`20`,
+	}, {
+		test("crypto/sha256", `len(sha256.Sum256("hash me"))`),
+		`32`,
+	}, {
+		test("crypto/sha256", `len(sha256.Sum224("hash me"))`),
+		`28`,
+	}, {
+		test("crypto/sha512", `len(sha512.Sum512("hash me"))`),
+		`64`,
+	}, {
+		test("crypto/sha512", `len(sha512.Sum384("hash me"))`),
+		`48`,
+	}, {
+		test("crypto/sha512", `len(sha512.Sum512_224("hash me"))`),
+		`28`,
+	}, {
+		test("crypto/sha512", `len(sha512.Sum512_256("hash me"))`),
+		`32`,
+	}, {
 		test("encoding/base64", `base64.Encode(null, "foo")`),
 		`"Zm9v"`,
 	}, {
diff --git a/cue/builtins.go b/cue/builtins.go
index e312947..33262ef 100644
--- a/cue/builtins.go
+++ b/cue/builtins.go
@@ -143,11 +143,12 @@
 		}, {
 			Name:   "Sum",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return md5.Sum(data)
+					a := md5.Sum(data)
+					return a[:]
 				}()
 			},
 		}},
@@ -162,11 +163,12 @@
 		}, {
 			Name:   "Sum",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return sha1.Sum(data)
+					a := sha1.Sum(data)
+					return a[:]
 				}()
 			},
 		}},
@@ -184,21 +186,23 @@
 		}, {
 			Name:   "Sum256",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return sha256.Sum256(data)
+					a := sha256.Sum256(data)
+					return a[:]
 				}()
 			},
 		}, {
 			Name:   "Sum224",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return sha256.Sum224(data)
+					a := sha256.Sum224(data)
+					return a[:]
 				}()
 			},
 		}},
@@ -222,41 +226,45 @@
 		}, {
 			Name:   "Sum512",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return sha512.Sum512(data)
+					a := sha512.Sum512(data)
+					return a[:]
 				}()
 			},
 		}, {
 			Name:   "Sum384",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return sha512.Sum384(data)
+					a := sha512.Sum384(data)
+					return a[:]
 				}()
 			},
 		}, {
 			Name:   "Sum512_224",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return sha512.Sum512_224(data)
+					a := sha512.Sum512_224(data)
+					return a[:]
 				}()
 			},
 		}, {
 			Name:   "Sum512_256",
 			Params: []kind{bytesKind | stringKind},
-			Result: topKind,
+			Result: bytesKind | stringKind,
 			Func: func(c *callCtxt) {
 				data := c.bytes(0)
 				c.ret = func() interface{} {
-					return sha512.Sum512_256(data)
+					a := sha512.Sum512_256(data)
+					return a[:]
 				}()
 			},
 		}},
diff --git a/pkg/crypto/md5/md5.go b/pkg/crypto/md5/md5.go
index 76abba2..d52c528 100644
--- a/pkg/crypto/md5/md5.go
+++ b/pkg/crypto/md5/md5.go
@@ -16,8 +16,6 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:generate qgo extract crypto/md5
-
 package md5
 
 import "crypto/md5"
@@ -29,6 +27,7 @@
 const BlockSize = 64
 
 // Sum returns the MD5 checksum of the data.
-func Sum(data []byte) [Size]byte {
-	return md5.Sum(data)
+func Sum(data []byte) []byte {
+	a := md5.Sum(data)
+	return a[:]
 }
diff --git a/pkg/crypto/sha1/sha1.go b/pkg/crypto/sha1/sha1.go
index f322690..de6a8b9 100644
--- a/pkg/crypto/sha1/sha1.go
+++ b/pkg/crypto/sha1/sha1.go
@@ -16,8 +16,6 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:generate qgo extract crypto/sha1
-
 package sha1
 
 import "crypto/sha1"
@@ -29,6 +27,7 @@
 const BlockSize = 64
 
 // Sum returns the SHA-1 checksum of the data.
-func Sum(data []byte) [Size]byte {
-	return sha1.Sum(data)
+func Sum(data []byte) []byte {
+	a := sha1.Sum(data)
+	return a[:]
 }
diff --git a/pkg/crypto/sha256/sha256.go b/pkg/crypto/sha256/sha256.go
index 85088ca..3320a06 100644
--- a/pkg/crypto/sha256/sha256.go
+++ b/pkg/crypto/sha256/sha256.go
@@ -16,8 +16,6 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:generate qgo extract crypto/sha256
-
 package sha256
 
 import "crypto/sha256"
@@ -32,11 +30,13 @@
 const BlockSize = 64
 
 // Sum256 returns the SHA256 checksum of the data.
-func Sum256(data []byte) [Size]byte {
-	return sha256.Sum256(data)
+func Sum256(data []byte) []byte {
+	a := sha256.Sum256(data)
+	return a[:]
 }
 
 // Sum224 returns the SHA224 checksum of the data.
-func Sum224(data []byte) (sum224 [Size224]byte) {
-	return sha256.Sum224(data)
+func Sum224(data []byte) (sum224 []byte) {
+	a := sha256.Sum224(data)
+	return a[:]
 }
diff --git a/pkg/crypto/sha512/sha512.go b/pkg/crypto/sha512/sha512.go
index 3d5db60..c010e53 100644
--- a/pkg/crypto/sha512/sha512.go
+++ b/pkg/crypto/sha512/sha512.go
@@ -16,8 +16,6 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:generate qgo extract crypto/sha512
-
 package sha512
 
 import "crypto/sha512"
@@ -41,21 +39,25 @@
 )
 
 // Sum512 returns the SHA512 checksum of the data.
-func Sum512(data []byte) [Size]byte {
-	return sha512.Sum512(data)
+func Sum512(data []byte) []byte {
+	a := sha512.Sum512(data)
+	return a[:]
 }
 
 // Sum384 returns the SHA384 checksum of the data.
-func Sum384(data []byte) (sum384 [Size384]byte) {
-	return sha512.Sum384(data)
+func Sum384(data []byte) (sum384 []byte) {
+	a := sha512.Sum384(data)
+	return a[:]
 }
 
 // Sum512_224 returns the Sum512/224 checksum of the data.
-func Sum512_224(data []byte) (sum224 [Size224]byte) {
-	return sha512.Sum512_224(data)
+func Sum512_224(data []byte) (sum224 []byte) {
+	a := sha512.Sum512_224(data)
+	return a[:]
 }
 
 // Sum512_256 returns the Sum512/256 checksum of the data.
-func Sum512_256(data []byte) (sum256 [Size256]byte) {
-	return sha512.Sum512_256(data)
+func Sum512_256(data []byte) (sum256 []byte) {
+	a := sha512.Sum512_256(data)
+	return a[:]
 }