aboutsummaryrefslogtreecommitdiff
path: root/lockchain-core/src/crypto/encoding.rs
blob: 0c49490d4446e582a1d6acdf200c9c93994aab1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//! Easy to use encoding utility functions

use base64;
use std::fmt::Write;

/// Encode a piece of arbitary data into a bse64 string
pub fn base64_encode(data: &Vec<u8>) -> String {
    return base64::encode(data);
}

/// Decode a base64 string into arbitrary data
pub fn base64_decode(data: &String) -> Vec<u8> {
    return base64::decode(data).unwrap();
}

/// Simply encode a byte-string as hexadecimal symbols
pub fn encode_hex(data: &str) -> String {
    let mut s = String::new();
    for &byte in data.as_bytes() {
        write!(&mut s, "{:X}", byte).expect("Unable to HEX encode!");
    }

    return s;
}