cfxstore/account/
kdf.rs

1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17use crate::json;
18
19#[derive(Debug, PartialEq, Clone)]
20pub enum Prf {
21    HmacSha256,
22}
23
24#[derive(Debug, PartialEq, Clone)]
25pub struct Pbkdf2 {
26    pub c: u32,
27    pub dklen: u32,
28    pub prf: Prf,
29    pub salt: Vec<u8>,
30}
31
32#[derive(Debug, PartialEq, Clone)]
33pub struct Scrypt {
34    pub dklen: u32,
35    pub p: u32,
36    pub n: u32,
37    pub r: u32,
38    pub salt: Vec<u8>,
39}
40
41#[derive(Debug, PartialEq, Clone)]
42pub enum Kdf {
43    Pbkdf2(Pbkdf2),
44    Scrypt(Scrypt),
45}
46
47impl From<json::Prf> for Prf {
48    fn from(json: json::Prf) -> Self {
49        match json {
50            json::Prf::HmacSha256 => Prf::HmacSha256,
51        }
52    }
53}
54
55impl Into<json::Prf> for Prf {
56    fn into(self) -> json::Prf {
57        match self {
58            Prf::HmacSha256 => json::Prf::HmacSha256,
59        }
60    }
61}
62
63impl From<json::Pbkdf2> for Pbkdf2 {
64    fn from(json: json::Pbkdf2) -> Self {
65        Pbkdf2 {
66            c: json.c,
67            dklen: json.dklen,
68            prf: From::from(json.prf),
69            salt: json.salt.into(),
70        }
71    }
72}
73
74impl Into<json::Pbkdf2> for Pbkdf2 {
75    fn into(self) -> json::Pbkdf2 {
76        json::Pbkdf2 {
77            c: self.c,
78            dklen: self.dklen,
79            prf: self.prf.into(),
80            salt: From::from(self.salt),
81        }
82    }
83}
84
85impl From<json::Scrypt> for Scrypt {
86    fn from(json: json::Scrypt) -> Self {
87        Scrypt {
88            dklen: json.dklen,
89            p: json.p,
90            n: json.n,
91            r: json.r,
92            salt: json.salt.into(),
93        }
94    }
95}
96
97impl Into<json::Scrypt> for Scrypt {
98    fn into(self) -> json::Scrypt {
99        json::Scrypt {
100            dklen: self.dklen,
101            p: self.p,
102            n: self.n,
103            r: self.r,
104            salt: From::from(self.salt),
105        }
106    }
107}
108
109impl From<json::Kdf> for Kdf {
110    fn from(json: json::Kdf) -> Self {
111        match json {
112            json::Kdf::Pbkdf2(params) => Kdf::Pbkdf2(From::from(params)),
113            json::Kdf::Scrypt(params) => Kdf::Scrypt(From::from(params)),
114        }
115    }
116}
117
118impl Into<json::Kdf> for Kdf {
119    fn into(self) -> json::Kdf {
120        match self {
121            Kdf::Pbkdf2(params) => json::Kdf::Pbkdf2(params.into()),
122            Kdf::Scrypt(params) => json::Kdf::Scrypt(params.into()),
123        }
124    }
125}