Skip to content

Commit ce08135

Browse files
committed
Add Password type
Password is a newtype for String that zeros out memory when dropped.
1 parent 7434f40 commit ce08135

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

key/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ heapsize = "0.4"
1515
rlp = { path = "../util/rlp" }
1616
secp256k1 = { path = "../util/secp256k1" }
1717
serde = "1.0"
18+
serde_derive = "1.0"
1819
serde_json = "1.0"
1920

2021
[features]

key/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern crate rustc_hex;
2626
extern crate rustc_serialize;
2727
extern crate secp256k1;
2828
extern crate serde;
29+
#[macro_use]
30+
extern crate serde_derive;
2931
extern crate serde_json;
3032

3133
mod address;
@@ -35,6 +37,7 @@ mod error;
3537
mod exchange;
3638
mod keypair;
3739
mod network;
40+
mod password;
3841
mod private;
3942
mod random;
4043
#[cfg(feature = "schnorr")]
@@ -50,6 +53,7 @@ pub use error::Error;
5053
pub use exchange::exchange;
5154
pub use keypair::{public_to_address, KeyPair};
5255
pub use network::Network;
56+
pub use password::Password;
5357
use primitives::{H160, H256, H512};
5458
pub use private::Private;
5559
pub use random::Random;

key/src/password.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2018 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program 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 Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use std::{fmt, ptr};
18+
19+
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
20+
pub struct Password(String);
21+
22+
impl fmt::Debug for Password {
23+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24+
write!(f, "Password(******)")
25+
}
26+
}
27+
28+
// Custom drop impl to zero out memory.
29+
impl Drop for Password {
30+
fn drop(&mut self) {
31+
unsafe {
32+
for byte_ref in self.0.as_mut_vec() {
33+
ptr::write_volatile(byte_ref, 0)
34+
}
35+
}
36+
}
37+
}
38+
39+
impl From<String> for Password {
40+
fn from(s: String) -> Password {
41+
Password(s)
42+
}
43+
}
44+
45+
impl<'a> From<&'a str> for Password {
46+
fn from(s: &'a str) -> Password {
47+
Password::from(String::from(s))
48+
}
49+
}

0 commit comments

Comments
 (0)