Select Git revision
-
Anders Blomdell authoredAnders Blomdell authored
hash.rs 4.23 KiB
/*
Copyright (C) 2018 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of hashtoc.
Hashtoc is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use libc::{c_char, c_int, size_t, time_t};
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::io::{Error, Result};
use std::os::unix::fs::{MetadataExt};
use std::fs::{Metadata};
#[link(name = "hash")]
extern {
fn hash_md5_file(path: *const c_char,
mtime: time_t,
mtime_nsec: i64,
flags: c_int,
maxage: time_t,
hash: &mut [u8;16]) -> c_int;
}
pub fn md5_file(path: &Path,
metadata: Metadata,
flags: c_int,
maxage: time_t) -> Result<String>
{
let mut hash: [u8;16] = [0;16];
let cstring = CString::new(path.as_os_str().as_bytes()).unwrap();
if unsafe { hash_md5_file(cstring.as_ptr(),
metadata.mtime(), metadata.mtime_nsec(), flags,
maxage, &mut hash) } < 0 {
Err(Error::last_os_error())
} else {
let s: Vec<String> = hash.iter().map(
|b| format!("{:02x}", b)).collect();
Ok(s.join(""))
}
}
#[link(name = "hash")]
extern {
fn hash_md5_buf(buf: *const c_char,
length: size_t,
hash: &mut [u8;16]) -> c_int;
}
pub fn md5_symlink(path: &Path) -> Result<String>
{
match path.read_link() {
Ok(symlink) => {
let cstring = CString::new(symlink.as_os_str().as_bytes()).unwrap();
let length = cstring.to_bytes().len();
let ptr = cstring.as_ptr();
let mut hash: [u8;16] = [0;16];
if unsafe { hash_md5_buf(ptr, length, &mut hash) } < 0 {
Err(Error::last_os_error())
} else {
let s: Vec<String> = hash.iter().map(
|b| format!("{:02x}", b)).collect();
Ok(s.join(""))
}
},
Err(e) => Err(e)
}
}
#[link(name = "hash")]
extern {
fn hash_sha512_file(path: *const c_char,
mtime: time_t,
mtime_nsec: i64,
flags: c_int,
maxage: time_t,
hash: &mut [u8;64]) -> c_int;
}
pub fn sha512_file(path: &Path,
metadata: Metadata,
flags: c_int,
maxage: time_t) -> Result<String>
{
let mut hash: [u8;64] = [0;64];
let cstring = CString::new(path.as_os_str().as_bytes()).unwrap();
if unsafe { hash_sha512_file(cstring.as_ptr(),
metadata.mtime(), metadata.mtime_nsec(), flags,
maxage, &mut hash) } < 0 {
Err(Error::last_os_error())
} else {
let s: Vec<String> = hash.iter().map(
|b| format!("{:02x}", b)).collect();
Ok(s.join(""))
}
}
#[link(name = "hash")]
extern {
fn hash_sha512_buf(buf: *const c_char,
length: size_t,
hash: &mut [u8;64]) -> c_int;
}
pub fn sha512_symlink(path: &Path) -> Result<String>
{
match path.read_link() {
Ok(symlink) => {
let cstring = CString::new(symlink.as_os_str().as_bytes()).unwrap();
let length = cstring.to_bytes().len();
let ptr = cstring.as_ptr();
let mut hash: [u8;64] = [0;64];
if unsafe { hash_sha512_buf(ptr, length, &mut hash) } < 0 {
Err(Error::last_os_error())
} else {
let s: Vec<String> = hash.iter().map(
|b| format!("{:02x}", b)).collect();
Ok(s.join(""))
}
},
Err(e) => Err(e)
}
}