rev: b5263db721ebca6ff9509f69a7255c55f9c956e4 tukan/lib/tukan/SHA256.sc -rw-r--r-- 2.3 KiB View raw Log this file
b5263db721eb — Leonard Ritter * initial work on base encoding 29 days ago
                                                                                
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using import struct

using import .crypto

unlet SHA224 SHA256

struct SHA224 plain
    let DigestType = (array u32 7)
    static-assert (SHA224_DIGEST_LENGTH == (sizeof DigestType))

    inline __typecall (cls)
        local self = (super-type.__typecall cls)
        SHA224_Init &self.ctx
        self

    fn... hash
    case (self : (mutable &this-type), data : rawstring, len : usize)
        SHA224_Update &self.ctx (data as @u8) len

    inline... digest
    case (self : (mutable &this-type),)
        local data : DigestType
        SHA224_Final (&data as (mutable @u8)) &self.ctx
        data
    case (self : (mutable &this-type), data : (mutable &DigestType))
        SHA224_Final (&data as (mutable @u8)) &self.ctx
        data

    ctx : SHA256_CTX

fn... sha224-digest-string
case (hval : (mutable &SHA224.DigestType),)
    let sz = (SHA224_DIGEST_LENGTH * 2)
    local str : (array i8 sz)
    inline conv (x)
        + x
            ? (x < 10:i8) 48:i8 87:i8
    for i in (range SHA224_DIGEST_LENGTH)
        c := (deref (hval @ i)) as i8
        i := i << 1
        str @ i = (conv ((c >> 4:i8) & 0xf:i8))
        str @ (i + 1) = (conv (c & 0xf:i8))
    string &str sz

inline... sha224
case (data : rawstring, len : usize)
    local sha : SHA224
    'hash sha data len
    'digest sha
case (data : string,)
    this-function data ((countof data) as usize)

struct SHA256 plain
    let DigestType = (array u64 4)
    static-assert (SHA256_DIGEST_LENGTH == (sizeof DigestType))

    inline __typecall (cls)
        local self = (super-type.__typecall cls)
        SHA256_Init &self.ctx
        self

    fn... hash
    case (self : (mutable &this-type), data : rawstring, len : usize)
        SHA256_Update &self.ctx (data as @u8) len

    inline... digest
    case (self : (mutable &this-type),)
        local data : DigestType
        SHA256_Final (&data as (mutable @u8)) &self.ctx
        data
    case (self : (mutable &this-type), data : (mutable &DigestType))
        SHA256_Final (&data as (mutable @u8)) &self.ctx
        data

    ctx : SHA256_CTX

inline... sha256
case (data : rawstring, len : usize)
    local sha : SHA256
    'hash sha data len
    'digest sha
case (data : string,)
    this-function data ((countof data) as usize)

do
    let sha224 SHA224 sha256 SHA256
    let sha224-digest-string
    locals;