-
Notifications
You must be signed in to change notification settings - Fork 8
/
md5.go
67 lines (50 loc) · 1.25 KB
/
md5.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/md5"
. "github.com/apmckinlay/gsuneido/core"
)
// 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.
type suMd5 struct {
ValueBase[suMd5]
hash hash.Hash
}
var _ = builtin(Md5, "(@args)")
func Md5(th *Thread, as *ArgSpec, args []Value) Value {
h := suMd5{hash: md5.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 = suMd5{}
func (h suMd5) Equal(other any) bool {
return h == other
}
func (suMd5) Lookup(_ *Thread, method string) Callable {
return md5Methods[method]
}
var md5Methods = methods()
var _ = method(md5_Update, "(string)")
func md5_Update(this, arg Value) Value {
io.WriteString(this.(suMd5).hash, ToStr(arg))
return this
}
var _ = method(md5_Value, "()")
func md5_Value(this Value) Value {
return this.(suMd5).value()
}
func (h suMd5) value() Value {
var buf [md5.Size]byte
return SuStr(string(h.hash.Sum(buf[0:0])))
}