Diff
Not logged in

Differences From Artifact [25f2937d3f]:

To Artifact [a9a3d52548]:


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
..
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

65
66
67
68
69


70

71
72
73
74
75
76
77
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
...
112
113
114
115
116
117
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
...
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
178
179
180
181
182
183
184

185
186
187
188
189
190
191
192


193

194
195
196
197
198
199
200
// Copyright 2015 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.0.  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.

use core::id::{IdType, RevocationIdType};
use routing::XorName;
use rust_sodium::crypto::{box_, sign};
use rust_sodium::crypto::hash::sha256;

/// PublicIdType
///
/// #Examples
///
/// ```
/// use ::safe_core::core::id::{IdType, RevocationIdType, MaidTypeTags, PublicIdType};
///
/// let revocation_maid = RevocationIdType::new::<MaidTypeTags>();
/// let maid = IdType::new(&revocation_maid);
/// let _public_maid  = PublicIdType::new(&maid, &revocation_maid);
/// ```
#[derive(Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable)]
pub struct PublicIdType {
    type_tag: u64,
    public_keys: (sign::PublicKey, box_::PublicKey),
    revocation_public_key: sign::PublicKey,
    signature: sign::Signature,
}

................................................................................
        let type_tag = revocation_id.type_tags().2;
        let public_keys = id_type.public_keys();
        let revocation_public_key = revocation_id.public_key();
        let combined_iter = (public_keys.0)
            .0
            .into_iter()
            .chain((public_keys.1)
                .0
                .into_iter()
                .chain(revocation_public_key.0.into_iter()));
        let mut combined: Vec<u8> = Vec::new();
        for iter in combined_iter {
            combined.push(*iter);
        }
        for i in type_tag.to_string().into_bytes().into_iter() {
            combined.push(i);
        }
        let message_length = combined.len();

        let signature = revocation_id.sign(&combined)

            .into_iter()
            .skip(message_length)
            .collect::<Vec<_>>();
        let mut signature_arr = [0; sign::SIGNATUREBYTES];



        for it in signature.into_iter().take(sign::SIGNATUREBYTES).enumerate() {

            signature_arr[it.0] = it.1;
        }

        PublicIdType {
            type_tag: type_tag,
            public_keys: *public_keys,
            revocation_public_key: *revocation_id.public_key(),
................................................................................
            .0
            .into_iter()
            .chain((self.public_keys.1).0.into_iter());
        let mut combined: Vec<u8> = Vec::new();
        for iter in combined_iter {
            combined.push(*iter);
        }
        for i in self.type_tag.to_string().into_bytes().into_iter() {
            combined.push(i);
        }
        for i in 0..sign::SIGNATUREBYTES {
            combined.push(self.signature.0[i]);
        }
        XorName(sha256::hash(&combined).0)
    }
................................................................................
    pub fn signature(&self) -> &sign::Signature {
        &self.signature
    }
}

#[cfg(test)]
mod test {

    use core::id::{IdType, MaidTypeTags, MpidTypeTags, Random, RevocationIdType};
    use maidsafe_utilities::serialisation::{deserialise, serialise};
    use rust_sodium::crypto::sign;
    use super::*;

    impl Random for PublicIdType {
        fn generate_random() -> PublicIdType {
            let revocation_maid = RevocationIdType::new::<MaidTypeTags>();
            let maid = IdType::new(&revocation_maid);
            PublicIdType::new(&maid, &revocation_maid)
        }
    }

    #[test]
    fn create_public_mpid() {
        let revocation_mpid = RevocationIdType::new::<MpidTypeTags>();
        let mpid = IdType::new(&revocation_mpid);
        let _ = PublicIdType::new(&mpid, &revocation_mpid);
    }

    #[test]
    fn serialisation_public_maid() {
        let obj_before: PublicIdType = Random::generate_random();

................................................................................
        let public_maid_third = PublicIdType::generate_random();
        assert_eq!(public_maid_first, public_maid_second);
        assert!(public_maid_first != public_maid_third);
    }

    #[test]
    fn invariant_check() {
        let revocation_maid = RevocationIdType::new::<MaidTypeTags>();
        let maid = IdType::new(&revocation_maid);
        let public_maid = PublicIdType::new(&maid, &revocation_maid);
        let type_tag = public_maid.type_tag;
        let public_id_keys = public_maid.public_keys;
        let public_revocation_key = public_maid.revocation_public_key;
        let combined_keys = (public_id_keys.0)
            .0
            .into_iter()
            .chain((public_id_keys.1)
                .0
                .into_iter()
                .chain(public_revocation_key.0
                    .into_iter()));
        let mut combined = Vec::new();

        for iter in combined_keys {
            combined.push(*iter);
        }
        for i in type_tag.to_string().into_bytes().into_iter() {
            combined.push(i);
        }

        let message_length = combined.len();
        let signature_vec = revocation_maid.sign(&combined)

            .into_iter()
            .skip(message_length)
            .collect::<Vec<_>>();

        assert_eq!(signature_vec.len(), sign::SIGNATUREBYTES);

        let mut signature_arr = [0; sign::SIGNATUREBYTES];



        for it in signature_vec.into_iter().take(sign::SIGNATUREBYTES).enumerate() {

            signature_arr[it.0] = it.1;
        }

        let signature = sign::Signature(signature_arr);
        assert!(&signature.0[..] == &public_maid.signature().0[..]);
    }
}







|
|













|






|
|


|







 







|
|
|




|




|
>





>
>
|
>







 







|







 







>



<



|
|






|
|







 







|
|








|
|
|
<





|




|
>








>
>
|
>







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
..
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
..
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
116
117
118
119
120
121
122
123
124
125
126

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2015 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.

use core::id::{IdType, RevocationIdType};
use routing::XorName;
use rust_sodium::crypto::{box_, sign};
use rust_sodium::crypto::hash::sha256;

/// `PublicIdType`
///
/// #Examples
///
/// ```
/// use ::safe_core::core::id::{IdType, RevocationIdType, MaidTypeTags, PublicIdType};
///
/// let revocation_maid = RevocationIdType::new::<MaidTypeTags>(None);
/// let maid = IdType::new(&revocation_maid, None);
/// let _public_maid  = PublicIdType::new(&maid, &revocation_maid);
/// ```
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PublicIdType {
    type_tag: u64,
    public_keys: (sign::PublicKey, box_::PublicKey),
    revocation_public_key: sign::PublicKey,
    signature: sign::Signature,
}

................................................................................
        let type_tag = revocation_id.type_tags().2;
        let public_keys = id_type.public_keys();
        let revocation_public_key = revocation_id.public_key();
        let combined_iter = (public_keys.0)
            .0
            .into_iter()
            .chain((public_keys.1)
                       .0
                       .into_iter()
                       .chain(revocation_public_key.0.into_iter()));
        let mut combined: Vec<u8> = Vec::new();
        for iter in combined_iter {
            combined.push(*iter);
        }
        for i in type_tag.to_string().into_bytes() {
            combined.push(i);
        }
        let message_length = combined.len();

        let signature = revocation_id
            .sign(&combined)
            .into_iter()
            .skip(message_length)
            .collect::<Vec<_>>();
        let mut signature_arr = [0; sign::SIGNATUREBYTES];

        for it in signature
                .into_iter()
                .take(sign::SIGNATUREBYTES)
                .enumerate() {
            signature_arr[it.0] = it.1;
        }

        PublicIdType {
            type_tag: type_tag,
            public_keys: *public_keys,
            revocation_public_key: *revocation_id.public_key(),
................................................................................
            .0
            .into_iter()
            .chain((self.public_keys.1).0.into_iter());
        let mut combined: Vec<u8> = Vec::new();
        for iter in combined_iter {
            combined.push(*iter);
        }
        for i in self.type_tag.to_string().into_bytes() {
            combined.push(i);
        }
        for i in 0..sign::SIGNATUREBYTES {
            combined.push(self.signature.0[i]);
        }
        XorName(sha256::hash(&combined).0)
    }
................................................................................
    pub fn signature(&self) -> &sign::Signature {
        &self.signature
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use core::id::{IdType, MaidTypeTags, MpidTypeTags, Random, RevocationIdType};
    use maidsafe_utilities::serialisation::{deserialise, serialise};
    use rust_sodium::crypto::sign;


    impl Random for PublicIdType {
        fn generate_random() -> PublicIdType {
            let revocation_maid = RevocationIdType::new::<MaidTypeTags>(None);
            let maid = IdType::new(&revocation_maid, None);
            PublicIdType::new(&maid, &revocation_maid)
        }
    }

    #[test]
    fn create_public_mpid() {
        let revocation_mpid = RevocationIdType::new::<MpidTypeTags>(None);
        let mpid = IdType::new(&revocation_mpid, None);
        let _ = PublicIdType::new(&mpid, &revocation_mpid);
    }

    #[test]
    fn serialisation_public_maid() {
        let obj_before: PublicIdType = Random::generate_random();

................................................................................
        let public_maid_third = PublicIdType::generate_random();
        assert_eq!(public_maid_first, public_maid_second);
        assert!(public_maid_first != public_maid_third);
    }

    #[test]
    fn invariant_check() {
        let revocation_maid = RevocationIdType::new::<MaidTypeTags>(None);
        let maid = IdType::new(&revocation_maid, None);
        let public_maid = PublicIdType::new(&maid, &revocation_maid);
        let type_tag = public_maid.type_tag;
        let public_id_keys = public_maid.public_keys;
        let public_revocation_key = public_maid.revocation_public_key;
        let combined_keys = (public_id_keys.0)
            .0
            .into_iter()
            .chain((public_id_keys.1)
                       .0
                       .into_iter()
                       .chain(public_revocation_key.0.into_iter()));

        let mut combined = Vec::new();

        for iter in combined_keys {
            combined.push(*iter);
        }
        for i in type_tag.to_string().into_bytes() {
            combined.push(i);
        }

        let message_length = combined.len();
        let signature_vec = revocation_maid
            .sign(&combined)
            .into_iter()
            .skip(message_length)
            .collect::<Vec<_>>();

        assert_eq!(signature_vec.len(), sign::SIGNATUREBYTES);

        let mut signature_arr = [0; sign::SIGNATUREBYTES];

        for it in signature_vec
                .into_iter()
                .take(sign::SIGNATUREBYTES)
                .enumerate() {
            signature_arr[it.0] = it.1;
        }

        let signature = sign::Signature(signature_arr);
        assert!(&signature.0[..] == &public_maid.signature().0[..]);
    }
}