/* 
Function to compute file hash
*/
void getHash(char *file, char *hashStr) {
 	int c;
 	unsigned char hash[SHA_DIGEST_LENGTH];
 	//char hash_as_string[SHA_DIGEST_LENGTH*2+1];

 	SHA_CTX ctx;
 	FILE *fp;

  //printf("should be full path: %s\n", file);
  
 	if ((fp=fopen(file, "r"))==NULL) {
    		fprintf(stderr, "error opening %s for reading\n", *fp);
    }
    SHA1_Init(&ctx);
   	
   	while ((c=fgetc(fp))!=EOF) {
   		SHA1_Update(&ctx, &c, 1);
   	}

   	SHA1_Final(hash, &ctx);
   	//printf("Made it here\n");
   	to_hex_string(hash, hashStr, SHA_DIGEST_LENGTH);

    fclose(fp);


   	//return hashStr;

}
Esempio n. 2
0
		bool authenticate(const std::string& plain,const std::string& challenge)
		{
			bool authenticated=(to_hex_string(hmac_sha3_512(key_m,plain+std::to_string(nonce_m)))==challenge);

			if(authenticated)
				++nonce_m;

			return authenticated;
		}
Esempio n. 3
0
// fills arrays, initializes visited array with 0's, meaning none of the
// files have been visited yet
void get_data(char *pathname, int depth, int *increment,
              int *visited, path *pathlist, hash *hashlist) {
    if (is_dir(pathname)) {
        DIR *d;
        struct dirent *p;
        char path[MAX_PATH_LENGTH];
        
        if ((d = opendir(pathname)) == NULL){
            fprintf(stderr, "opendir %s  %s\n", path, strerror(errno));
            return;
        }

        while ((p = readdir(d)) != NULL) {
            if (strcmp(".", p->d_name)==0 || strcmp("..", p->d_name)==0)
                continue;
            snprintf(path, MAX_PATH_LENGTH, "%s/%s", pathname, p->d_name);
            get_data(path, depth+1, &(*increment), visited, 
                     pathlist, hashlist);
        }
        closedir(d);
    }
    else if(depth>0) {
        SHA_CTX ctx;
        FILE *fp;
        int c;
        unsigned char hash[SHA_DIGEST_LENGTH];
        if ((fp=fopen(pathname, "r"))==NULL) {
            fprintf(stderr, "error opening %s for reading\n", pathname);
            return;
        }
        
        SHA1_Init(&ctx);
        while((c=fgetc(fp))!=EOF)
            SHA1_Update(&ctx, &c, 1);
        
        SHA1_Final(hash, &ctx);
        
        to_hex_string(hash, *(hashlist+(*increment)), SHA_DIGEST_LENGTH);
        *(visited+(*increment))=0;
        strncpy(*(pathlist+(*increment)), pathname, MAX_PATH_LENGTH);
        
        (*increment)++;
    }
}
Esempio n. 4
0
 /// Set the size of the chunk.
 /// @param size the size of the chunk in bytes.
 void set_size(size_t size)
 {
   size_ = size;
   hex_size_ = to_hex_string(size);
 }
Esempio n. 5
0
 /// Encoding constructor.
 /// Set the chunk size and optionally the extension.
 /// @param size the size of the chunk in bytes.
 /// @param extension the chunk extension (default blank).
 explicit chunk_header(size_t size,
                       std::string extension = "")
   : size_(size)
   , hex_size_(to_hex_string(size))
   , extension_(extension)
 {}
Esempio n. 6
0
/// \brief Get a string with the hex representation of the given byte.
inline
std::string to_hex_string(signed char Byte) {
  return to_hex_string(static_cast<unsigned char>(Byte));
}
Esempio n. 7
0
ncap_record_list_t
ncap_base_record_t::from_disk(std::vector< uint8_t >& vec)
{
	ncap_record_list_t 	list;
	const std::size_t	min(sizeof(uint64_t)*4);

	for (std::size_t off = 0; off < vec.size(); ) {
		uint64_t				magic(0);
		uint64_t 				id(0);
		uint64_t				type(NCAP_INVALID_RECORD);
		uint64_t 				length(0);
		std::vector< uint8_t > 	data;

		
		if (vec.size() >= off && (vec.size() - off) < min)
			throw std::runtime_error("ncap_base_record_t::from_disk(): Malformed input file encountered (Min)");

		magic	= *reinterpret_cast< uint64_t* >(&vec[off]);

		if (NCAP_MAGIC != magic)
			throw std::runtime_error("ncap_base_record_t::from_disk(): Malformed input file encountered (Magic)");

		id 		= *reinterpret_cast< uint64_t* >(&vec[off] + sizeof(magic));
		type	= *reinterpret_cast< uint64_t* >(&vec[off] + sizeof(magic) + sizeof(id));
		length	= *reinterpret_cast< uint64_t* >(&vec[off] + sizeof(magic) + sizeof(id) + sizeof(type));

		off += min;

		id		= ncap_base_record_t::byte_swap(id);
		type	= ncap_base_record_t::byte_swap(type);
		length	= ncap_base_record_t::byte_swap(length);

		if (false == ncap_base_record_t::is_valid_type(type & NCAP_RECORD_TYPE_MASK))
			throw std::runtime_error("ncap_base_record_t::from_disk(): Malformed input file encountered (Type): " + to_hex_string(type));

		if (vec.size() >= off && (vec.size() - off) < length)
			throw std::runtime_error("ncap_base_record_t::from_disk(): Malformed input file encountered (Length)");

		data.resize(length);
		std::memcpy(data.data(), &vec[off], length);
		off += length;
		list.push_back(ncap_base_record_t(id, type, data));
	}

	return list;
}
Esempio n. 8
0
std::string PCI_id::str() const
{
	return to_hex_string(vendor, 4, false) + ":" + to_hex_string(device, 4, false);
}
Esempio n. 9
0
std::string PCI_id::device_hex() const
{
	return to_hex_string(device, 4);
}
Esempio n. 10
0
std::string PCI_id::vendor_hex() const
{
	return to_hex_string(vendor, 4);
}