Skip to content

Commit 1c2e4d7

Browse files
faukahamaanq
authored andcommitted
nar: drop b64 function, replace occurences by data_encoding::BASE64.encode()
1 parent 9614305 commit 1c2e4d7

1 file changed

Lines changed: 3 additions & 31 deletions

File tree

src/nar.rs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,24 @@ use anyhow::{
2020
Context as _,
2121
Result,
2222
};
23+
use data_encoding::BASE64;
2324
use sha2::{
2425
Digest as _,
2526
Sha256,
2627
};
2728

28-
const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29-
3029
/// sri nar hash of `root`, matching builtins.fetchTree
3130
pub fn hash_path(root: &Path) -> Result<String> {
3231
let mut hash = Sha256::new();
3332
emit_bytes(&mut hash, b"nix-archive-1");
3433
let mut path = root.to_path_buf();
3534
emit_node(&mut hash, &mut path)?;
36-
Ok(format!("sha256-{}", b64(&hash.finalize())))
35+
Ok(format!("sha256-{}", BASE64.encode(&hash.finalize())))
3736
}
3837

3938
/// sri sha256 of raw bytes; matches what `builtin:fetchurl` checks against
4039
pub fn hash_bytes(bytes: &[u8]) -> String {
41-
format!("sha256-{}", b64(&Sha256::digest(bytes)))
40+
format!("sha256-{}", BASE64.encode(&Sha256::digest(bytes)))
4241
}
4342

4443
fn emit_node(hash: &mut Sha256, path: &mut PathBuf) -> Result<()> {
@@ -115,30 +114,3 @@ fn pad(hasher: &mut Sha256, len: u64) {
115114
hasher.update(&[0_u8; 8][..8 - rem]);
116115
}
117116
}
118-
119-
fn b64(data: &[u8]) -> String {
120-
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
121-
for chunk in data.chunks(3) {
122-
let triple = [
123-
chunk[0],
124-
*chunk.get(1).unwrap_or(&0),
125-
*chunk.get(2).unwrap_or(&0),
126-
];
127-
let packed = (u32::from(triple[0]) << 16_u32)
128-
| (u32::from(triple[1]) << 8_u32)
129-
| u32::from(triple[2]);
130-
out.push(B64[((packed >> 18_u32) & 63_u32) as usize] as char);
131-
out.push(B64[((packed >> 12_u32) & 63_u32) as usize] as char);
132-
out.push(if chunk.len() > 1 {
133-
B64[((packed >> 6_u32) & 63_u32) as usize] as char
134-
} else {
135-
'='
136-
});
137-
out.push(if chunk.len() > 2 {
138-
B64[(packed & 63_u32) as usize] as char
139-
} else {
140-
'='
141-
});
142-
}
143-
out
144-
}

0 commit comments

Comments
 (0)