summaryrefslogtreecommitdiffstats
path: root/vendor/spdx-rs/src/models/checksum.rs
blob: d66bd36126821027cd7895081236abb0ce8426c9 (plain)
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
// SPDX-FileCopyrightText: 2020-2021 HH Partners
//
// SPDX-License-Identifier: MIT

use serde::{Deserialize, Serialize};

/// Representation of SPDX's
/// [Package Checksum](https://spdx.github.io/spdx-spec/3-package-information/#310-package-checksum)
/// and
/// [File Checksum](https://spdx.github.io/spdx-spec/4-file-information/#44-file-checksum).
/// According to the spec, SHA1 is mandatory but we don't currently enforce that.
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Clone)]
pub struct Checksum {
    /// Algorithm used to calculate the checksum
    pub algorithm: Algorithm,

    /// The checksum value.
    #[serde(rename = "checksumValue")]
    pub value: String,
}

impl Checksum {
    /// Create new checksum.
    pub fn new(algorithm: Algorithm, value: &str) -> Self {
        Self {
            algorithm,
            value: value.to_lowercase(),
        }
    }
}

/// Possible algorithms to be used for SPDX's
/// [package checksum](https://spdx.github.io/spdx-spec/3-package-information/#310-package-checksum)
/// and [file checksum](https://spdx.github.io/spdx-spec/4-file-information/#44-file-checksum).
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Clone, Copy)]
pub enum Algorithm {
    SHA1,
    SHA224,
    SHA256,
    SHA384,
    SHA512,
    MD2,
    MD4,
    MD5,
    MD6,
    #[serde(rename = "SHA3-256")]
    SHA3256,
    #[serde(rename = "SHA3-384")]
    SHA3384,
    #[serde(rename = "SHA3-512")]
    SHA3512,
    #[serde(rename = "BLAKE2b-256")]
    BLAKE2B256,
    #[serde(rename = "BLAKE2b-384")]
    BLAKE2B384,
    #[serde(rename = "BLAKE2b-512")]
    BLAKE2B512,
    BLAKE3,
    ADLER32,
}