mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
crypto md5
This commit is contained in:
committed by
Alexander Medvednikov
parent
8c516bec4f
commit
9c586e7e92
@@ -3,16 +3,17 @@
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Package sha1 implements the SHA-1 hash algorithm as defined in RFC 3174.
|
||||
//
|
||||
|
||||
// SHA-1 is cryptographically broken and should not be used for secure
|
||||
// applications.
|
||||
|
||||
// Adapted from: https://github.com/golang/go/blob/master/src/crypto/sha1
|
||||
|
||||
module sha1
|
||||
|
||||
import math
|
||||
import encoding.binary
|
||||
|
||||
|
||||
const(
|
||||
// The size of a SHA-1 checksum in bytes.
|
||||
Size = 20
|
||||
@@ -68,7 +69,7 @@ pub fn (d mut Digest) write(p []byte) ?int {
|
||||
}
|
||||
d.nx += n
|
||||
if d.nx == Chunk {
|
||||
block(d, d.x)
|
||||
block_generic(d, d.x)
|
||||
d.nx = 0
|
||||
}
|
||||
if n >= p.len {
|
||||
@@ -79,7 +80,7 @@ pub fn (d mut Digest) write(p []byte) ?int {
|
||||
}
|
||||
if p.len >= Chunk {
|
||||
n := p.len &~ (Chunk - 1)
|
||||
block(d, p.left(n))
|
||||
block_generic(d, p.left(n))
|
||||
if n >= p.len {
|
||||
p = []byte
|
||||
} else {
|
||||
@@ -98,14 +99,14 @@ pub fn (d mut Digest) write(p []byte) ?int {
|
||||
pub fn (d &Digest) sum(b_in mut []byte) []byte {
|
||||
// Make a copy of d so that caller can keep writing and summing.
|
||||
mut d0 := *d
|
||||
hash := d0.check_sum()
|
||||
hash := d0.checksum()
|
||||
for b in hash {
|
||||
b_in << b
|
||||
}
|
||||
return *b_in
|
||||
}
|
||||
|
||||
fn (d mut Digest) check_sum() []byte {
|
||||
fn (d mut Digest) checksum() []byte {
|
||||
mut len := d.len
|
||||
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
||||
mut tmp := [byte(0); 64]
|
||||
@@ -136,10 +137,9 @@ fn (d mut Digest) check_sum() []byte {
|
||||
|
||||
// Sum returns the SHA-1 checksum of the data.
|
||||
pub fn sum(data []byte) []byte {
|
||||
mut d := Digest{}
|
||||
d.reset()
|
||||
mut d := new()
|
||||
d.write(data)
|
||||
return d.check_sum()
|
||||
return d.checksum()
|
||||
}
|
||||
|
||||
pub fn (d &Digest) size() int { return Size }
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
// 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.
|
||||
|
||||
import crypto.sha1
|
||||
|
||||
fn test_crypto_sha1() {
|
||||
assert sha1.sum('This is a sha1 hash.'.bytes()).hex() == '6FF5FA4D5166D5C2576FE56ED1EC2D5AB0FDF936'
|
||||
assert sha1.sum('This is a sha1 checksum.'.bytes()).hex() == 'E100D74442FAA5DCD59463B808983C810A8EB5A1'
|
||||
}
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
// 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.
|
||||
|
||||
// This is a generic implementation with no arch optimizations
|
||||
|
||||
module sha1
|
||||
|
||||
import math.bits
|
||||
@@ -9,7 +15,7 @@ const (
|
||||
_K3 = 0xCA62C1D6
|
||||
)
|
||||
|
||||
fn block(dig &Digest, p []byte) {
|
||||
fn block_generic(dig &Digest, p []byte) {
|
||||
mut w := [u32(0); 16]
|
||||
mut h0 := dig.h[0]
|
||||
mut h1 := dig.h[1]
|
||||
@@ -73,13 +79,11 @@ fn block(dig &Digest, p []byte) {
|
||||
w[i&0xf] = u32(tmp<<u32(1)) | u32(tmp>>u32(32-1))
|
||||
f := ((b | c) & d) | (b & c)
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_K2)
|
||||
|
||||
e = d
|
||||
d = c
|
||||
c = bits.rotate_left_32(b, 30)
|
||||
b = a
|
||||
a = t
|
||||
|
||||
i++
|
||||
}
|
||||
for i < 80 {
|
||||
Reference in New Issue
Block a user