Diff
Not logged in

Differences From Artifact [57004c0590]:

To Artifact [94e101ec6e]:


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
..
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
123
124
125
126
127
128
129

130
131
132
133
134
135
136
137
138
139
140
...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.1.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

/// Maximum allowed length for a [message's `body`](struct.MpidMessage.html#method.new) (101,760
/// bytes).
pub const MAX_BODY_SIZE: usize = 102400 - 512 - super::MAX_HEADER_METADATA_SIZE;


use maidsafe_utilities::serialisation::serialise;
use rust_sodium::crypto::sign::{self, PublicKey, SecretKey, Signature};
use std::fmt::{self, Debug, Formatter};
use super::{Error, MpidHeader};
use utils;
use xor_name::XorName;

#[derive(PartialEq, Eq, Hash, Clone, RustcDecodable, RustcEncodable)]
struct Detail {
    recipient: XorName,
    body: Vec<u8>,
}

/// A full message including header and body which can be sent to or retrieved from the network.
#[derive(PartialEq, Eq, Hash, Clone, RustcDecodable, RustcEncodable)]
pub struct MpidMessage {
    header: MpidHeader,
    detail: Detail,
    signature: Signature,
}

impl MpidMessage {
................................................................................
        let detail = Detail {
            recipient: recipient,
            body: body,
        };

        let recipient_and_body = serialise(&detail)?;
        Ok(MpidMessage {
            header: header,
            detail: detail,
            signature: sign::sign_detached(&recipient_and_body, secret_key),
        })
    }

    /// Getter for `MpidHeader` member, created when calling `new()`.
    pub fn header(&self) -> &MpidHeader {
        &self.header
    }

................................................................................
               utils::format_binary_array(&self.detail.body),
               utils::format_binary_array(&self.signature))
    }
}

#[cfg(test)]
mod tests {

    use messaging;
    use rand;
    use rust_sodium::crypto::sign;
    use super::*;
    use xor_name::XorName;

    #[test]
    fn full() {
        let (mut public_key, secret_key) = sign::gen_keypair();
        let sender: XorName = rand::random();
        let metadata = messaging::generate_random_bytes(messaging::MAX_HEADER_METADATA_SIZE);
................................................................................
        }
        let mut body = messaging::generate_random_bytes(MAX_BODY_SIZE);
        let message = unwrap!(MpidMessage::new(sender.clone(),
                                               metadata.clone(),
                                               recipient.clone(),
                                               body.clone(),
                                               &secret_key));
        assert!(*message.body() == body);
        body.push(0);
        assert!(MpidMessage::new(sender.clone(),
                                 metadata.clone(),
                                 recipient.clone(),
                                 body.clone(),
                                 &secret_key)
            .is_err());
        let _ = body.pop();

        // Check verify function with a valid and invalid key
        assert!(message.verify(&public_key));
        if public_key.0[0] != 255 {
            public_key.0[0] += 1;
        } else {
            public_key.0[0] = 0;
        }
        assert!(!message.verify(&public_key));
    }
}







|
|












>



<



|






|







 







|
|
|
|







 







>



<







 







|






|












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
..
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
123
124
125
126
127
128
129
130
131
132
133

134
135
136
137
138
139
140
...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

/// Maximum allowed length for a [message's `body`](struct.MpidMessage.html#method.new) (101,760
/// bytes).
pub const MAX_BODY_SIZE: usize = 102400 - 512 - super::MAX_HEADER_METADATA_SIZE;

use super::{Error, MpidHeader};
use maidsafe_utilities::serialisation::serialise;
use rust_sodium::crypto::sign::{self, PublicKey, SecretKey, Signature};
use std::fmt::{self, Debug, Formatter};

use utils;
use xor_name::XorName;

#[derive(PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
struct Detail {
    recipient: XorName,
    body: Vec<u8>,
}

/// A full message including header and body which can be sent to or retrieved from the network.
#[derive(PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
pub struct MpidMessage {
    header: MpidHeader,
    detail: Detail,
    signature: Signature,
}

impl MpidMessage {
................................................................................
        let detail = Detail {
            recipient: recipient,
            body: body,
        };

        let recipient_and_body = serialise(&detail)?;
        Ok(MpidMessage {
               header: header,
               detail: detail,
               signature: sign::sign_detached(&recipient_and_body, secret_key),
           })
    }

    /// Getter for `MpidHeader` member, created when calling `new()`.
    pub fn header(&self) -> &MpidHeader {
        &self.header
    }

................................................................................
               utils::format_binary_array(&self.detail.body),
               utils::format_binary_array(&self.signature))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use messaging;
    use rand;
    use rust_sodium::crypto::sign;

    use xor_name::XorName;

    #[test]
    fn full() {
        let (mut public_key, secret_key) = sign::gen_keypair();
        let sender: XorName = rand::random();
        let metadata = messaging::generate_random_bytes(messaging::MAX_HEADER_METADATA_SIZE);
................................................................................
        }
        let mut body = messaging::generate_random_bytes(MAX_BODY_SIZE);
        let message = unwrap!(MpidMessage::new(sender.clone(),
                                               metadata.clone(),
                                               recipient.clone(),
                                               body.clone(),
                                               &secret_key));
        assert_eq!(*message.body(), body);
        body.push(0);
        assert!(MpidMessage::new(sender.clone(),
                                 metadata.clone(),
                                 recipient.clone(),
                                 body.clone(),
                                 &secret_key)
                        .is_err());
        let _ = body.pop();

        // Check verify function with a valid and invalid key
        assert!(message.verify(&public_key));
        if public_key.0[0] != 255 {
            public_key.0[0] += 1;
        } else {
            public_key.0[0] = 0;
        }
        assert!(!message.verify(&public_key));
    }
}