Skip to content
Snippets Groups Projects
Commit 4dc000c5 authored by Éloïs's avatar Éloïs
Browse files

[ref] rename module : dewip -> dewif

parent 9b712c5e
No related branches found
No related tags found
No related merge requests found
...@@ -29,8 +29,8 @@ zeroize = { version = "1.1.0", features = ["zeroize_derive"] } ...@@ -29,8 +29,8 @@ zeroize = { version = "1.1.0", features = ["zeroize_derive"] }
bincode = "1.2.0" bincode = "1.2.0"
[features] [features]
default = ["dewip", "ser"] default = ["dewif", "ser"]
aes256 = ["aes"] aes256 = ["aes"]
dewip = ["aes256", "arrayvec"] dewif = ["aes256", "arrayvec"]
ser = ["serde"] ser = ["serde"]
...@@ -13,12 +13,12 @@ ...@@ -13,12 +13,12 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Handle [DEWIP](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) format //! Handle [DEWIF](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) format
//! //!
//! # Write ed25519 key-pair in DEWIF file //! # Write ed25519 key-pair in DEWIF file
//! //!
//! ``` //! ```
//! use dup_crypto::dewip::write_dewif_v1_content; //! use dup_crypto::dewif::write_dewif_v1_content;
//! use dup_crypto::keys::ed25519::{KeyPairFromSaltedPasswordGenerator, SaltedPassword}; //! use dup_crypto::keys::ed25519::{KeyPairFromSaltedPasswordGenerator, SaltedPassword};
//! //!
//! // Get user credentials (from cli prompt or gui) //! // Get user credentials (from cli prompt or gui)
...@@ -42,18 +42,18 @@ ...@@ -42,18 +42,18 @@
//! # Read DEWIF file //! # Read DEWIF file
//! //!
//! ``` //! ```
//! use dup_crypto::dewip::read_dewip_file_content; //! use dup_crypto::dewif::read_dewif_file_content;
//! use dup_crypto::keys::{KeyPair, Signator}; //! use dup_crypto::keys::{KeyPair, Signator};
//! //!
//! // Get DEWIP file content (Usually from disk) //! // Get DEWIF file content (Usually from disk)
//! let dewip_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA="; //! let dewif_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA=";
//! //!
//! // Get user passphrase for DEWIF decryption (from cli prompt or gui) //! // Get user passphrase for DEWIF decryption (from cli prompt or gui)
//! let encryption_passphrase = "toto titi tata"; //! let encryption_passphrase = "toto titi tata";
//! //!
//! // Read DEWIP file content //! // Read DEWIF file content
//! // If the file content is correct, we get a key-pair iterator. //! // If the file content is correct, we get a key-pair iterator.
//! let mut key_pair_iter = read_dewip_file_content(dewip_file_content, encryption_passphrase) //! let mut key_pair_iter = read_dewif_file_content(dewif_file_content, encryption_passphrase)
//! .expect("invalid DEWIF file.") //! .expect("invalid DEWIF file.")
//! .into_iter(); //! .into_iter();
//! //!
...@@ -85,7 +85,7 @@ ...@@ -85,7 +85,7 @@
mod read; mod read;
mod write; mod write;
pub use read::{read_dewip_file_content, DewipReadError}; pub use read::{read_dewif_file_content, DewifReadError};
pub use write::{write_dewif_v1_content, write_dewif_v2_content}; pub use write::{write_dewif_v1_content, write_dewif_v2_content};
use crate::hashs::Hash; use crate::hashs::Hash;
...@@ -132,13 +132,13 @@ mod tests { ...@@ -132,13 +132,13 @@ mod tests {
use crate::seeds::Seed32; use crate::seeds::Seed32;
#[test] #[test]
fn dewip_v1() { fn dewif_v1() {
let written_keypair = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let written_keypair = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32]));
let dewif_content = write_dewif_v1_content(&written_keypair, "toto"); let dewif_content = write_dewif_v1_content(&written_keypair, "toto");
let mut keypairs_iter = read_dewip_file_content(&dewif_content, "toto") let mut keypairs_iter = read_dewif_file_content(&dewif_content, "toto")
.expect("dewip content must be readed successfully") .expect("dewif content must be readed successfully")
.into_iter(); .into_iter();
let keypair_read = keypairs_iter.next().expect("Must read one keypair"); let keypair_read = keypairs_iter.next().expect("Must read one keypair");
...@@ -146,7 +146,7 @@ mod tests { ...@@ -146,7 +146,7 @@ mod tests {
} }
#[test] #[test]
fn dewip_v1_corrupted() -> Result<(), ()> { fn dewif_v1_corrupted() -> Result<(), ()> {
let written_keypair = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let written_keypair = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32]));
let mut dewif_content = write_dewif_v1_content(&written_keypair, "toto"); let mut dewif_content = write_dewif_v1_content(&written_keypair, "toto");
...@@ -155,8 +155,8 @@ mod tests { ...@@ -155,8 +155,8 @@ mod tests {
let dewif_bytes_mut = unsafe { dewif_content.as_bytes_mut() }; let dewif_bytes_mut = unsafe { dewif_content.as_bytes_mut() };
dewif_bytes_mut[13] = 0x52; dewif_bytes_mut[13] = 0x52;
if let Err(DewipReadError::CorruptedContent) = if let Err(DewifReadError::CorruptedContent) =
read_dewip_file_content(&dewif_content, "toto") read_dewif_file_content(&dewif_content, "toto")
{ {
Ok(()) Ok(())
} else { } else {
...@@ -165,14 +165,14 @@ mod tests { ...@@ -165,14 +165,14 @@ mod tests {
} }
#[test] #[test]
fn dewip_v2() { fn dewif_v2() {
let written_keypair1 = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let written_keypair1 = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32]));
let written_keypair2 = KeyPairFromSeed32Generator::generate(Seed32::new([1u8; 32])); let written_keypair2 = KeyPairFromSeed32Generator::generate(Seed32::new([1u8; 32]));
let dewif_content = write_dewif_v2_content(&written_keypair1, &written_keypair2, "toto"); let dewif_content = write_dewif_v2_content(&written_keypair1, &written_keypair2, "toto");
let mut keypairs_iter = read_dewip_file_content(&dewif_content, "toto") let mut keypairs_iter = read_dewif_file_content(&dewif_content, "toto")
.expect("dewip content must be readed successfully") .expect("dewif content must be readed successfully")
.into_iter(); .into_iter();
let keypair1_read = keypairs_iter.next().expect("Must read one keypair"); let keypair1_read = keypairs_iter.next().expect("Must read one keypair");
let keypair2_read = keypairs_iter.next().expect("Must read one keypair"); let keypair2_read = keypairs_iter.next().expect("Must read one keypair");
...@@ -182,7 +182,7 @@ mod tests { ...@@ -182,7 +182,7 @@ mod tests {
} }
#[test] #[test]
fn dewip_v2_corrupted() -> Result<(), ()> { fn dewif_v2_corrupted() -> Result<(), ()> {
let written_keypair1 = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let written_keypair1 = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32]));
let written_keypair2 = KeyPairFromSeed32Generator::generate(Seed32::new([1u8; 32])); let written_keypair2 = KeyPairFromSeed32Generator::generate(Seed32::new([1u8; 32]));
...@@ -193,8 +193,8 @@ mod tests { ...@@ -193,8 +193,8 @@ mod tests {
let dewif_bytes_mut = unsafe { dewif_content.as_bytes_mut() }; let dewif_bytes_mut = unsafe { dewif_content.as_bytes_mut() };
dewif_bytes_mut[13] = 0x52; dewif_bytes_mut[13] = 0x52;
if let Err(DewipReadError::CorruptedContent) = if let Err(DewifReadError::CorruptedContent) =
read_dewip_file_content(&dewif_content, "toto") read_dewif_file_content(&dewif_content, "toto")
{ {
Ok(()) Ok(())
} else { } else {
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Read [DEWIP](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content //! Read [DEWIF](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content
use crate::keys::ed25519::{KeyPairFromSeed32Generator, PublicKey, PUBKEY_SIZE_IN_BYTES}; use crate::keys::ed25519::{KeyPairFromSeed32Generator, PublicKey, PUBKEY_SIZE_IN_BYTES};
use crate::keys::KeyPairEnum; use crate::keys::KeyPairEnum;
...@@ -25,11 +25,11 @@ use thiserror::Error; ...@@ -25,11 +25,11 @@ use thiserror::Error;
const MAX_KEYPAIRS_COUNT: usize = 2; const MAX_KEYPAIRS_COUNT: usize = 2;
/// Error when try to read DEWIP file content /// Error when try to read DEWIF file content
#[derive(Clone, Debug, Error)] #[derive(Clone, Debug, Error)]
pub enum DewipReadError { pub enum DewifReadError {
/// DEWIP file content is corrupted /// DEWIF file content is corrupted
#[error("DEWIP file content is corrupted")] #[error("DEWIF file content is corrupted")]
CorruptedContent, CorruptedContent,
/// Invalid base 64 string /// Invalid base 64 string
#[error("Invalid base 64 string: {0}")] #[error("Invalid base 64 string: {0}")]
...@@ -51,15 +51,15 @@ pub enum DewipReadError { ...@@ -51,15 +51,15 @@ pub enum DewipReadError {
}, },
} }
/// read dewip file content with user passphrase /// read dewif file content with user passphrase
pub fn read_dewip_file_content( pub fn read_dewif_file_content(
file_content: &str, file_content: &str,
passphrase: &str, passphrase: &str,
) -> Result<impl IntoIterator<Item = KeyPairEnum>, DewipReadError> { ) -> Result<impl IntoIterator<Item = KeyPairEnum>, DewifReadError> {
let mut bytes = base64::decode(file_content).map_err(DewipReadError::InvalidBase64Str)?; let mut bytes = base64::decode(file_content).map_err(DewifReadError::InvalidBase64Str)?;
if bytes.len() < 4 { if bytes.len() < 4 {
return Err(DewipReadError::TooShortContent); return Err(DewifReadError::TooShortContent);
} }
let version = byteorder::BigEndian::read_u32(&bytes[0..4]); let version = byteorder::BigEndian::read_u32(&bytes[0..4]);
...@@ -67,20 +67,20 @@ pub fn read_dewip_file_content( ...@@ -67,20 +67,20 @@ pub fn read_dewip_file_content(
match version { match version {
1 => Ok({ 1 => Ok({
let mut array_keypairs = ArrayVec::new(); let mut array_keypairs = ArrayVec::new();
array_keypairs.push(read_dewip_v1(&mut bytes[4..], passphrase)?); array_keypairs.push(read_dewif_v1(&mut bytes[4..], passphrase)?);
array_keypairs array_keypairs
}), }),
2 => read_dewip_v2(&mut bytes[4..], passphrase), 2 => read_dewif_v2(&mut bytes[4..], passphrase),
other_version => Err(DewipReadError::UnsupportedVersion { other_version => Err(DewifReadError::UnsupportedVersion {
actual: other_version, actual: other_version,
}), }),
} }
} }
fn read_dewip_v1(bytes: &mut [u8], passphrase: &str) -> Result<KeyPairEnum, DewipReadError> { fn read_dewif_v1(bytes: &mut [u8], passphrase: &str) -> Result<KeyPairEnum, DewifReadError> {
match bytes.len() { match bytes.len() {
len if len < super::V1_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooShortContent), len if len < super::V1_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooShortContent),
len if len > super::V1_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooLongContent), len if len > super::V1_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooLongContent),
_ => (), _ => (),
} }
...@@ -92,15 +92,15 @@ fn read_dewip_v1(bytes: &mut [u8], passphrase: &str) -> Result<KeyPairEnum, Dewi ...@@ -92,15 +92,15 @@ fn read_dewip_v1(bytes: &mut [u8], passphrase: &str) -> Result<KeyPairEnum, Dewi
bytes_to_checked_keypair(bytes) bytes_to_checked_keypair(bytes)
} }
fn read_dewip_v2( fn read_dewif_v2(
bytes: &mut [u8], bytes: &mut [u8],
passphrase: &str, passphrase: &str,
) -> Result<ArrayVec<[KeyPairEnum; MAX_KEYPAIRS_COUNT]>, DewipReadError> { ) -> Result<ArrayVec<[KeyPairEnum; MAX_KEYPAIRS_COUNT]>, DewifReadError> {
let mut array_keypairs = ArrayVec::new(); let mut array_keypairs = ArrayVec::new();
match bytes.len() { match bytes.len() {
len if len < super::V2_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooShortContent), len if len < super::V2_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooShortContent),
len if len > super::V2_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooLongContent), len if len > super::V2_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooLongContent),
_ => (), _ => (),
} }
...@@ -114,7 +114,7 @@ fn read_dewip_v2( ...@@ -114,7 +114,7 @@ fn read_dewip_v2(
Ok(array_keypairs) Ok(array_keypairs)
} }
fn bytes_to_checked_keypair(bytes: &[u8]) -> Result<KeyPairEnum, DewipReadError> { fn bytes_to_checked_keypair(bytes: &[u8]) -> Result<KeyPairEnum, DewifReadError> {
// Wrap bytes into Seed32 and PublicKey // Wrap bytes into Seed32 and PublicKey
let seed = Seed32::new( let seed = Seed32::new(
(&bytes[..PUBKEY_SIZE_IN_BYTES]) (&bytes[..PUBKEY_SIZE_IN_BYTES])
...@@ -128,7 +128,7 @@ fn bytes_to_checked_keypair(bytes: &[u8]) -> Result<KeyPairEnum, DewipReadError> ...@@ -128,7 +128,7 @@ fn bytes_to_checked_keypair(bytes: &[u8]) -> Result<KeyPairEnum, DewipReadError>
// Check pubkey // Check pubkey
if keypair.pubkey() != expected_pubkey { if keypair.pubkey() != expected_pubkey {
Err(DewipReadError::CorruptedContent) Err(DewifReadError::CorruptedContent)
} else { } else {
Ok(KeyPairEnum::Ed25519(keypair)) Ok(KeyPairEnum::Ed25519(keypair))
} }
...@@ -141,7 +141,7 @@ mod tests { ...@@ -141,7 +141,7 @@ mod tests {
#[test] #[test]
fn read_unsupported_version() -> Result<(), ()> { fn read_unsupported_version() -> Result<(), ()> {
if let Err(DewipReadError::UnsupportedVersion { .. }) = read_dewip_file_content( if let Err(DewifReadError::UnsupportedVersion { .. }) = read_dewif_file_content(
"ABAAAfKjMzOFfhwgypF3mAx0QDXyozMzhX4cIMqRd5gMdEA1WZwQjCR49iZDK2QhYfdTbPz9AGB01edt4iRSzdTp3c4=", "ABAAAfKjMzOFfhwgypF3mAx0QDXyozMzhX4cIMqRd5gMdEA1WZwQjCR49iZDK2QhYfdTbPz9AGB01edt4iRSzdTp3c4=",
"toto" "toto"
) { ) {
...@@ -153,7 +153,7 @@ mod tests { ...@@ -153,7 +153,7 @@ mod tests {
#[test] #[test]
fn read_too_short_content() -> Result<(), ()> { fn read_too_short_content() -> Result<(), ()> {
if let Err(DewipReadError::TooShortContent) = read_dewip_file_content("AAA", "toto") { if let Err(DewifReadError::TooShortContent) = read_dewif_file_content("AAA", "toto") {
Ok(()) Ok(())
} else { } else {
panic!("Read must be fail with error TooShortContent.") panic!("Read must be fail with error TooShortContent.")
...@@ -164,15 +164,15 @@ mod tests { ...@@ -164,15 +164,15 @@ mod tests {
fn tmp() { fn tmp() {
use crate::keys::{KeyPair, Signator}; use crate::keys::{KeyPair, Signator};
// Get DEWIP file content (Usually from disk) // Get DEWIF file content (Usually from disk)
let dewip_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA="; let dewif_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA=";
// Get user passphrase for DEWIF decryption (from cli prompt or gui) // Get user passphrase for DEWIF decryption (from cli prompt or gui)
let encryption_passphrase = "toto titi tata"; let encryption_passphrase = "toto titi tata";
// Read DEWIP file content // Read DEWIF file content
// If the file content is correct, we get a key-pair iterator. // If the file content is correct, we get a key-pair iterator.
let mut key_pair_iter = read_dewip_file_content(dewip_file_content, encryption_passphrase) let mut key_pair_iter = read_dewif_file_content(dewif_file_content, encryption_passphrase)
.expect("invalid DEWIF file.") .expect("invalid DEWIF file.")
.into_iter(); .into_iter();
......
...@@ -13,13 +13,13 @@ ...@@ -13,13 +13,13 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Write [DEWIP](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content //! Write [DEWIF](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content
use crate::keys::ed25519::Ed25519KeyPair; use crate::keys::ed25519::Ed25519KeyPair;
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use unwrap::unwrap; use unwrap::unwrap;
/// Write dewip v1 file content with user passphrase /// Write dewif v1 file content with user passphrase
pub fn write_dewif_v1_content(keypair: &Ed25519KeyPair, passphrase: &str) -> String { pub fn write_dewif_v1_content(keypair: &Ed25519KeyPair, passphrase: &str) -> String {
let mut bytes = ArrayVec::<[u8; super::V1_BYTES_LEN]>::new(); let mut bytes = ArrayVec::<[u8; super::V1_BYTES_LEN]>::new();
unwrap!(bytes.try_extend_from_slice(super::VERSION_V1)); unwrap!(bytes.try_extend_from_slice(super::VERSION_V1));
...@@ -32,7 +32,7 @@ pub fn write_dewif_v1_content(keypair: &Ed25519KeyPair, passphrase: &str) -> Str ...@@ -32,7 +32,7 @@ pub fn write_dewif_v1_content(keypair: &Ed25519KeyPair, passphrase: &str) -> Str
base64::encode(bytes.as_ref()) base64::encode(bytes.as_ref())
} }
/// Write dewip v2 file content with user passphrase /// Write dewif v2 file content with user passphrase
pub fn write_dewif_v2_content( pub fn write_dewif_v2_content(
keypair1: &Ed25519KeyPair, keypair1: &Ed25519KeyPair,
keypair2: &Ed25519KeyPair, keypair2: &Ed25519KeyPair,
......
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
#[cfg(feature = "aes256")] #[cfg(feature = "aes256")]
pub mod aes256; pub mod aes256;
pub mod bases; pub mod bases;
#[cfg(feature = "dewip")] #[cfg(feature = "dewif")]
pub mod dewip; pub mod dewif;
pub mod hashs; pub mod hashs;
pub mod keys; pub mod keys;
pub mod rand; pub mod rand;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment