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
 44
 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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
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
143
144
145
146
147
148
149
150
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::borrow::Borrow;
use std::convert::{Into, TryFrom};
use std::marker::PhantomData;
use std::{cmp, fmt, hash, ops};

use unsigned_varint::{decode as varint_decode, encode as varint_encode};

use crate::errors::{DecodeError, DecodeOwnedError};
use crate::hashes::Code;
use crate::storage::Storage;

// It would be nice if default generics would work well with `PhantomData`, so that instead of this
// custom type `Multihash<T = Code>` would work.
/// This type is using the default Multihash code table
pub type Multihash = MultihashGeneric<Code>;

/// This type is using the default Multihash code table
pub type MultihashRef<'a> = MultihashRefGeneric<'a, Code>;

/// This type makes it easier to interact with hashers
///
/// # Example
///
/// ```ignore
/// let hasher1: BoxedMultihashDigest = Code::Sha3_512.into();
/// let hasher2: BoxedMultihashDigest<_> = MyCodecTable::MyHash.into();
/// ```
pub type BoxedMultihashDigest<T = Code> = Box<dyn MultihashDigest<T>>;

/// Representation of a valid multihash. This enforces validity on construction,
/// so it can be assumed this is always a valid multihash.
///
/// This generic type can be used with your own code table.
///
/// # Example
///
/// ```
/// use multihash::{wrap, MultihashGeneric};
/// use std::convert::TryFrom;
///
/// #[derive(Debug)]
/// pub enum MyCodeTable {
///     Foo = 0x01,
///     Bar = 0x02,
/// }
///
/// impl TryFrom<u64> for MyCodeTable {
///     type Error = String;
///
///     fn try_from(raw: u64) -> Result<Self, Self::Error> {
///         match raw {
///             0x01 => Ok(Self::Foo),
///             0x02 => Ok(Self::Bar),
///             _ => Err("invalid code".to_string()),
///         }
///     }
/// }
///
/// impl From<MyCodeTable> for u64 {
///     fn from(code: MyCodeTable) -> Self {
///         code as u64
///     }
/// }
///
/// #[derive(Clone, Debug)]
/// struct SameHash;
/// impl SameHash {
///     pub const CODE: MyCodeTable = MyCodeTable::Foo;
///     /// Hash some input and return the sha1 digest.
///     pub fn digest(_data: &[u8]) -> MultihashGeneric<MyCodeTable> {
///         let digest = b"alwaysthesame";
///         wrap(Self::CODE, digest)
///     }
/// }
///
/// let my_hash = SameHash::digest(b"abc");
/// assert_eq!(my_hash.digest(), b"alwaysthesame");
/// ```
///
/// This mechanism can also be used if you want to extend the existing code table
///
/// # Example
///
/// ```
/// use multihash::Code;
/// use std::convert::TryFrom;
///
/// #[derive(Debug, PartialEq)]
/// enum ExtendedCode {
///     Foo,
///     Bar,
///     NormalCode(Code),
/// }
///
/// impl TryFrom<u64> for ExtendedCode {
///     type Error = String;
///
///     /// Return the `Code` based on the integer value
///     fn try_from(raw: u64) -> Result<Self, Self::Error> {
///         match raw {
///             0x01 => Ok(Self::Foo),
///             0x02 => Ok(Self::Bar),
///             // Fallback to the default values
///             _ => match Code::try_from(raw) {
///                 Ok(code) => Ok(Self::NormalCode(code)),
///                 Err(_) => Err("invalid code".to_string()),
///             }, //_ => Err("invalid code".to_string()),
///         }
///     }
/// }
///
/// impl From<ExtendedCode> for u64 {
///     fn from(code: ExtendedCode) -> Self {
///         match code {
///             ExtendedCode::Foo => 0x01,
///             ExtendedCode::Bar => 0x02,
///             ExtendedCode::NormalCode(normal_code) => normal_code.into(),
///         }
///     }
/// }
///
/// impl TryFrom<ExtendedCode> for Code {
///     type Error = String;
///
///     fn try_from(extended: ExtendedCode) -> Result<Self, Self::Error> {
///         match extended {
///             ExtendedCode::NormalCode(code) => Ok(code),
///             _ => Err("Not a default code".to_string()),
///         }
///     }
/// }
///
/// assert_eq!(ExtendedCode::try_from(0x02).unwrap(), ExtendedCode::Bar);
/// assert_eq!(
///     ExtendedCode::try_from(0x12).unwrap(),
///     ExtendedCode::NormalCode(Code::Sha2_256)
/// );
/// assert_eq!(
///     Code::try_from(ExtendedCode::try_from(0x12).unwrap()).unwrap(),
///     Code::Sha2_256
/// );
/// ```
#[derive(Clone)]
pub struct MultihashGeneric<T: TryFrom<u64>> {
    storage: Storage,
    // Use `PhantomData` in order to be able to make the `Multihash` struct take a generic
    _code: PhantomData<T>,
}

impl<T: TryFrom<u64>> fmt::Debug for MultihashGeneric<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Multihash").field(&self.as_bytes()).finish()
    }
}

impl<T: TryFrom<u64>> PartialEq for MultihashGeneric<T> {
    fn eq(&self, other: &Self) -> bool {
        self.storage.bytes() == other.storage.bytes()
    }
}

impl<T: TryFrom<u64>> Eq for MultihashGeneric<T> {}

impl<T: TryFrom<u64>> hash::Hash for MultihashGeneric<T> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.storage.bytes().hash(state);
    }
}

impl<T: TryFrom<u64>> MultihashGeneric<T> {
    /// Creates a new `Multihash` from a `Vec<u8>`, consuming it.
    /// If the input data is not a valid multihash an error is returned.
    ///
    /// # Example
    ///
    /// ```
    /// use multihash::{Multihash, Sha2_256};
    ///
    /// let mh = Sha2_256::digest(b"hello world");
    ///
    /// // valid multihash
    /// let mh2 = Multihash::from_bytes(mh.into_bytes()).unwrap();
    ///
    /// // invalid multihash
    /// assert!(Multihash::from_bytes(vec![1, 2, 3]).is_err());
    /// ```
    pub fn from_bytes(bytes: Vec<u8>) -> Result<MultihashGeneric<T>, DecodeOwnedError> {
        if let Err(err) = MultihashRefGeneric::<T>::from_slice(&bytes) {
            return Err(DecodeOwnedError {
                error: err,
                data: bytes,
            });
        }
        Ok(Self {
            storage: Storage::from_slice(&bytes),
            _code: PhantomData,
        })
    }

    /// Returns the bytes representation of the multihash.
    pub fn into_bytes(self) -> Vec<u8> {
        self.to_vec()
    }

    /// Returns the bytes representation of the multihash.
    pub fn to_vec(&self) -> Vec<u8> {
        Vec::from(self.as_bytes())
    }

    /// Returns the bytes representation of this multihash.
    pub fn as_bytes(&self) -> &[u8] {
        self.storage.bytes()
    }

    /// Builds a `MultihashRef` corresponding to this `Multihash`.
    pub fn as_ref(&self) -> MultihashRefGeneric<T> {
        MultihashRefGeneric {
            bytes: self.as_bytes(),
            _code: PhantomData,
        }
    }

    /// Returns the algorithm used in this multihash.
    ///
    /// # Example
    ///
    /// ```
    /// use multihash::{Code, Sha2_256};
    ///
    /// let mh = Sha2_256::digest(b"hello world");
    /// assert_eq!(mh.algorithm(), Code::Sha2_256);
    /// ```
    pub fn algorithm(&self) -> T {
        self.as_ref().algorithm()
    }

    /// Returns the hashed data.
    pub fn digest(&self) -> &[u8] {
        self.as_ref().digest()
    }
}

impl<T: TryFrom<u64>> AsRef<[u8]> for MultihashGeneric<T> {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl<T: TryFrom<u64>> ops::Deref for MultihashGeneric<T> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.as_bytes()
    }
}

impl<T: TryFrom<u64>> Borrow<[u8]> for MultihashGeneric<T> {
    fn borrow(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl<'a, T: TryFrom<u64>> PartialEq<MultihashRefGeneric<'a, T>> for MultihashGeneric<T> {
    fn eq(&self, other: &MultihashRefGeneric<'a, T>) -> bool {
        &*self.as_bytes() == other.as_bytes()
    }
}

impl<T: TryFrom<u64>> TryFrom<Vec<u8>> for MultihashGeneric<T> {
    type Error = DecodeOwnedError;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        MultihashGeneric::from_bytes(value)
    }
}

impl<T: TryFrom<u64>> Into<Vec<u8>> for MultihashGeneric<T> {
    fn into(self) -> Vec<u8> {
        self.to_vec()
    }
}

impl<T: TryFrom<u64>> PartialOrd for MultihashGeneric<T> {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: TryFrom<u64>> Ord for MultihashGeneric<T> {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.as_ref().cmp(&other.as_ref())
    }
}

/// Represents a valid multihash.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MultihashRefGeneric<'a, T> {
    bytes: &'a [u8],
    _code: PhantomData<T>,
}

impl<'a, T: TryFrom<u64>> MultihashRefGeneric<'a, T> {
    /// Creates a new `MultihashRef` from a `&[u8]`.
    /// If the input data is not a valid multihash an error is returned.
    ///
    /// # Example
    ///
    /// ```
    /// use multihash::{MultihashRef, Sha2_256};
    ///
    /// let mh = Sha2_256::digest(b"hello world");
    ///
    /// // valid multihash
    /// let mh2 = MultihashRef::from_slice(&mh).unwrap();
    ///
    /// // invalid multihash
    /// assert!(MultihashRef::from_slice(&vec![1, 2, 3]).is_err());
    /// ```
    pub fn from_slice(input: &'a [u8]) -> Result<Self, DecodeError> {
        if input.is_empty() {
            return Err(DecodeError::BadInputLength);
        }

        let (code, bytes) = varint_decode::u64(&input).map_err(|_| DecodeError::BadInputLength)?;
        // Make sure it's a code that is part of the codec table
        T::try_from(code).map_err(|_| DecodeError::UnknownCode)?;

        let (hash_len, bytes) =
            varint_decode::u64(&bytes).map_err(|_| DecodeError::BadInputLength)?;
        if (bytes.len() as u64) != hash_len {
            return Err(DecodeError::BadInputLength);
        }

        Ok(Self {
            bytes: input,
            _code: PhantomData,
        })
    }

    /// Returns the algorithm used in this multihash.
    ///
    /// # Example
    ///
    /// ```
    /// use multihash::{Code, MultihashRef, Sha2_256};
    ///
    /// let mh = Sha2_256::digest(b"hello world");
    ///
    /// // valid multihash
    /// let mh2 = MultihashRef::from_slice(&mh).unwrap();
    /// assert_eq!(mh2.algorithm(), Code::Sha2_256);
    /// ```
    pub fn algorithm(&self) -> T {
        let (rawcode, _bytes) =
            varint_decode::u64(&self.bytes).expect("multihash is known to be valid algorithm");
        T::try_from(rawcode)
            .unwrap_or_else(|_| panic!("Should not occur as multihash is known to be valid"))
    }

    /// Returns the hash digest.
    ///
    /// # Example
    ///
    /// ```
    /// use multihash::{wrap, Code, Multihash, Sha2_256};
    ///
    /// let mh = Sha2_256::digest(b"hello world");
    /// let digest = mh.digest();
    /// let wrapped: Multihash = wrap(Code::Sha2_256, &digest);
    /// assert_eq!(wrapped.digest(), digest);
    /// ```
    pub fn digest(&self) -> &'a [u8] {
        let (_code, bytes) =
            varint_decode::u64(&self.bytes).expect("multihash is known to be valid digest");
        let (_hash_len, bytes) =
            varint_decode::u64(&bytes).expect("multihash is known to be a valid digest");
        &bytes[..]
    }

    /// Builds a `Multihash` that owns the data.
    ///
    /// This operation allocates.
    pub fn to_owned(&self) -> MultihashGeneric<T> {
        MultihashGeneric {
            storage: Storage::from_slice(self.bytes),
            _code: PhantomData,
        }
    }

    /// Returns the bytes representation of this multihash.
    pub fn as_bytes(&self) -> &'a [u8] {
        &self.bytes
    }
}

impl<'a, T: TryFrom<u64>> PartialEq<MultihashGeneric<T>> for MultihashRefGeneric<'a, T> {
    fn eq(&self, other: &MultihashGeneric<T>) -> bool {
        self.as_bytes() == &*other.as_bytes()
    }
}

impl<'a, T: TryFrom<u64>> ops::Deref for MultihashRefGeneric<'a, T> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.as_bytes()
    }
}

impl<'a, T: TryFrom<u64>> Into<Vec<u8>> for MultihashRefGeneric<'a, T> {
    fn into(self) -> Vec<u8> {
        self.to_vec()
    }
}

/// The `Multihasher` trait specifies an interface common for all multihash functions
/// that does not require allocating a `Box<dyn MultihashDigest<T>>`.
pub trait Multihasher<T: TryFrom<u64> + Copy> {
    /// The multihash code.
    const CODE: T;

    /// Hash some input and return the digest.
    fn digest(data: &[u8]) -> MultihashGeneric<T>;
}

/// The `MultihashDigest` trait specifies an interface common for all multihash functions.
pub trait MultihashDigest<T: TryFrom<u64>> {
    /// The Mutlihash byte value.
    fn code(&self) -> T;

    /// Hash some input and return the digest.
    ///
    /// # Panics
    ///
    /// Panics if the digest length is bigger than 2^32. This only happens for identity hasing.
    fn digest(&self, data: &[u8]) -> MultihashGeneric<T>;

    /// Digest data, updating the internal state.
    ///
    /// This method can be called repeatedly for use with streaming messages.
    ///
    /// # Panics
    ///
    /// Panics if the digest length is bigger than 2^32. This only happens for identity hashing.
    fn input(&mut self, data: &[u8]);

    /// Retrieve the computed `MultihashGeneric`, consuming the hasher.
    fn result(self) -> MultihashGeneric<T>;

    /// Retrieve result and reset hasher instance.
    ///
    /// This method sometimes can be more efficient compared to hasher re-creation.
    fn result_reset(&mut self) -> MultihashGeneric<T>;

    /// Reset hasher instance to its initial state.
    fn reset(&mut self);
}

/// Wraps a hash digest in Multihash with the given Mutlihash code.
///
/// The size of the hash is determoned by the size of the input hash. If it should be truncated
/// the input data must already be the truncated hash.
///
/// # Example
///
/// ```
/// use multihash::{wrap, Code, Multihash, Sha2_256};
///
/// let mh = Sha2_256::digest(b"hello world");
/// let digest = mh.digest();
/// let wrapped: Multihash = wrap(Code::Sha2_256, &digest);
/// assert_eq!(wrapped.digest(), digest);
/// ```
pub fn wrap<T: Into<u64> + TryFrom<u64>>(code: T, data: &[u8]) -> MultihashGeneric<T> {
    let mut code_buf = varint_encode::u64_buffer();
    let code = varint_encode::u64(code.into(), &mut code_buf);

    let mut size_buf = varint_encode::u64_buffer();
    let size = varint_encode::u64(data.len() as u64, &mut size_buf);

    MultihashGeneric {
        storage: Storage::from_slices(&[code, &size, &data]),
        _code: PhantomData,
    }
}