1///|
2/// This file is based on the Go implementation found here:
3/// https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
4/// which has the copyright notice:
5/// Copyright 2009 The Go Authors. All rights reserved.
6/// Use of this source code is governed by a BSD-style
7/// license that can be found in the LICENSE file.
8///
9/// Package hash provides interfaces for hash functions.
10typealias Slice[T] = type @io.Slice[T]
A Slice is a slice of an Array.
A separate Slice type was needed because of this issue:
https://github.com/moonbitlang/core/issues/1063
This struct is based on MoonBit's "ArrayView" implementation here:
https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt
which has the following copyright:
Copyright 2024 International Digital Economy Academy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@io.Slice[type parameter T
T]
11
12///|
13typealias IOError = type! @io.IOError String
An IOError can be tested with equality checks.
@io.IOError
14
15///|
16/// Hash is the common interface implemented by all hash functions.
17///
18/// Hash implementations in the standard library (e.g. [hash/crc32] and
19/// [crypto/sha256]) implement the [encoding.BinaryMarshaler] and
20/// [encoding.BinaryUnmarshaler] interfaces. Marshaling a hash implementation
21/// allows its internal state to be saved and used for additional processing
22/// later, without having to re-write the data previously written to the hash.
23/// The hash state may contain portions of the input in its original form,
24/// which users are expected to handle for any possible security implications.
25///
26/// Compatibility: Any future changes to hash or crypto packages will endeavor
27/// to maintain compatibility with state encoded using previous versions.
28/// That is, any released versions of the packages should be able to
29/// decode data written with any previously released version,
30/// subject to issues such as security fixes.
31/// See the Go compatibility document for background: https://golang.org/doc/go1compat
32pub(open) trait trait Hash {
write(Self, @io.Slice[Byte]) -> (Int, @io.IOError?)
sum(Self, @io.Slice[Byte]) -> @io.Slice[Byte]
reset(Self) -> Unit
size(Self) -> Int
block_size(Self) -> Int
}
Hash is the common interface implemented by all hash functions.
Hash implementations in the standard library (e.g. [hash/crc32] and
[crypto/sha256]) implement the [encoding.BinaryMarshaler] and
[encoding.BinaryUnmarshaler] interfaces. Marshaling a hash implementation
allows its internal state to be saved and used for additional processing
later, without having to re-write the data previously written to the hash.
The hash state may contain portions of the input in its original form,
which users are expected to handle for any possible security implications.
Compatibility: Any future changes to hash or crypto packages will endeavor
to maintain compatibility with state encoded using previous versions.
That is, any released versions of the packages should be able to
decode data written with any previously released version,
subject to issues such as security fixes.
See the Go compatibility document for background: https://golang.org/doc/go1compat
Hash {
33 /// Write (via the embedded io.Writer interface) adds more data to the running hash.
34 /// It never returns an error.
35 /// @io.Writer
36 (Self, @io.Slice[Byte]) -> (Int, @io.IOError?)
write(type parameter Self
Self, type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]) -> (Int
Int, type! @io.IOError String
An IOError can be tested with equality checks.
IOError?)
37
38 /// sum appends the current hash to b and returns the resulting slice.
39 /// It does not change the underlying hash state.
40 (Self, @io.Slice[Byte]) -> @io.Slice[Byte]
sum(type parameter Self
Self, type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]) -> type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]
41
42 /// reset resets the Hash to its initial state.
43 (Self) -> Unit
reset(type parameter Self
Self) -> Unit
Unit
44
45 /// size returns the number of bytes sum will return.
46 (Self) -> Int
size(type parameter Self
Self) -> Int
Int
47
48 /// block_size returns the hash's underlying block size.
49 /// The Write method must be able to accept any amount
50 /// of data, but it may operate more efficiently if all writes
51 /// are a multiple of the block size.
52 (Self) -> Int
block_size(type parameter Self
Self) -> Int
Int
53}
54
55///|
56/// Hash32 is the common interface implemented by all 32-bit hash functions.
57pub(open) trait trait Hash32 {
sum32(Self) -> UInt
write(Self, @io.Slice[Byte]) -> (Int, @io.IOError?)
sum(Self, @io.Slice[Byte]) -> @io.Slice[Byte]
reset(Self) -> Unit
size(Self) -> Int
block_size(Self) -> Int
}
Hash32 is the common interface implemented by all 32-bit hash functions.
Hash32 {
58 (Self) -> UInt
sum32(type parameter Self
Self) -> UInt
UInt
59 // Hash
60 (Self, @io.Slice[Byte]) -> (Int, @io.IOError?)
write(type parameter Self
Self, type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]) -> (Int
Int, type! @io.IOError String
An IOError can be tested with equality checks.
IOError?)
61 (Self, @io.Slice[Byte]) -> @io.Slice[Byte]
sum(type parameter Self
Self, type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]) -> type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]
62 (Self) -> Unit
reset(type parameter Self
Self) -> Unit
Unit
63 (Self) -> Int
size(type parameter Self
Self) -> Int
Int
64 (Self) -> Int
block_size(type parameter Self
Self) -> Int
Int
65}
66
67///|
68/// Hash64 is the common interface implemented by all 64-bit hash functions.
69pub(open) trait trait Hash64 {
sum64() -> UInt64
write(Self, @io.Slice[Byte]) -> (Int, @io.IOError?)
sum(Self, @io.Slice[Byte]) -> @io.Slice[Byte]
reset(Self) -> Unit
size(Self) -> Int
block_size(Self) -> Int
}
Hash64 is the common interface implemented by all 64-bit hash functions.
Hash64 {
70 () -> UInt64
sum64() -> UInt64
UInt64
71 // Hash
72 (Self, @io.Slice[Byte]) -> (Int, @io.IOError?)
write(type parameter Self
Self, type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]) -> (Int
Int, type! @io.IOError String
An IOError can be tested with equality checks.
IOError?)
73 (Self, @io.Slice[Byte]) -> @io.Slice[Byte]
sum(type parameter Self
Self, type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]) -> type @io.Slice[T]
This file is based on the Go implementation found here:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/hash/hash.go;l=26
which has the copyright notice:
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Package hash provides interfaces for hash functions.
Slice[Byte
Byte]
74 (Self) -> Unit
reset(type parameter Self
Self) -> Unit
Unit
75 (Self) -> Int
size(type parameter Self
Self) -> Int
Int
76 (Self) -> Int
block_size(type parameter Self
Self) -> Int
Int
77}
78