/
sha256.go
67 lines (50 loc) · 1.31 KB
/
sha256.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright Suneido Software Corp. All rights reserved.
// Governed by the MIT license found in the LICENSE file.
package builtin
import (
"hash"
"io"
"crypto/sha256"
. "github.com/apmckinlay/gsuneido/core"
)
type suSha256 struct {
ValueBase[suSha256]
hash hash.Hash
}
// The built-in hashes are Adler32, Md5, Sha1, Sha256.
// The implementations are very similar.
// Modifications to any of them should probably be done to the others.
var _ = builtin(Sha256, "(@args)")
func Sha256(th *Thread, as *ArgSpec, args []Value) Value {
h := suSha256{hash: sha256.New()}
iter := NewArgsIter(as, args)
k, v := iter()
if v == nil {
return h
}
for ; k == nil && v != nil; k, v = iter() {
io.WriteString(h.hash, ToStr(v))
}
return h.value()
}
var _ Value = suSha256{}
func (h suSha256) Equal(other any) bool {
return h == other
}
func (suSha256) Lookup(_ *Thread, method string) Callable {
return sha256Methods[method]
}
var sha256Methods = methods()
var _ = method(Sha256_Update, "(string)")
func Sha256_Update(this, arg Value) Value {
io.WriteString(this.(suSha256).hash, ToStr(arg))
return this
}
var _ = method(Sha256_Value, "()")
func Sha256_Value(this Value) Value {
return this.(suSha256).value()
}
func (h suSha256) value() Value {
var buf [sha256.Size]byte
return SuStr(string(h.hash.Sum(buf[0:0])))
}