1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sha1 implementation + helper funcs

This commit is contained in:
joe-conigliaro
2019-07-16 01:49:01 +10:00
committed by Alexander Medvednikov
parent 37aff9b107
commit a7529b7b05
9 changed files with 454 additions and 3 deletions

View File

@ -13,6 +13,11 @@ const (
Koopman = 0xeb31d82e
)
// The size of a CRC-32 checksum in bytes.
const (
Size = 4
)
struct Crc32 {
mut:
table []u32
@ -32,7 +37,7 @@ fn(c mut Crc32) generate_table(poly int) {
}
}
fn(c &Crc32) sum_32(s string) u32 {
fn(c &Crc32) sum32(s string) u32 {
mut crc := ~u32(0)
for i := 0; i < s.len; i++ {
crc = c.table[byte(crc)^s[i]] ^ u32(crc >> u32(8))
@ -41,7 +46,7 @@ fn(c &Crc32) sum_32(s string) u32 {
}
pub fn(c &Crc32) checksum(s string) u32 {
return c.sum_32(s)
return c.sum32(s)
}
// pass the polinomial to use
@ -54,6 +59,6 @@ pub fn new(poly int) *Crc32 {
// calculate crc32 using IEEE
pub fn sum(s string) u32 {
mut c := new(IEEE)
return c.sum_32(s)
return c.sum32(s)
}

21
vlib/hash/hash.v Normal file
View File

@ -0,0 +1,21 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module hash
interface Hash {
// Sum appends the current hash to b and returns the resulting array.
// It does not change the underlying hash state.
sum(b []byte) []byte
size() int
block_size() int
}
interface Hash32 {
sum32() uint32
}
interface Hash64 {
sum64() uint64
}