Пример #1
0
/*! \brief Compute MD5 checksum */
void compute_md5(char *dst, char *src, int src_len)
{
	MD5_CTX context;
	unsigned char digest[16];
	MD5Init (&context);
  	MD5Update (&context, src, src_len);
	U_MD5Final (digest, &context);
	string2hex(digest, 16, dst);
}
Пример #2
0
/*! \brief
  * Calculate a MD5 digests over a string array and stores
  * the result in the destination char array.
  * This function assumes 32 bytes in the destination buffer.
  * \param dest destination
  * \param src string input array
  * \param size elements in the input array
  */
void MD5StringArray(char *dest, str src[], unsigned int size)
{
	MD5_CTX context;
	unsigned char digest[16];
	int i, len;
	char *tmp;

	MD5Init (&context);
	for (i=0; i < size; i++) {
		trim_len(len, tmp, src[i]);
		MD5Update(&context, tmp, len);
	}
	MD5Final(digest, &context);

	string2hex(digest, 16, dest);
	LM_DBG("MD5 calculated: %.*s\n", MD5_LEN, dest);
}
Пример #3
0
/*! \brief
  * Calculate a MD5 digest over a file.
  * This function assumes 32 bytes in the destination buffer.
  * \param dest destination
  * \param file_name file for that the digest should be calculated
  * \return zero on success, negative on errors
  */
static int MD5File(char *dest, const char *file_name)
{
	MD5_CTX context;
	FILE *input;
	unsigned char buffer[32768];
	unsigned char hash[16];
	unsigned int counter, size;
	
	struct stat stats;
	
	if (!dest || !file_name) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

    if (stat(file_name, &stats) != 0) {
		LM_ERR("could not stat file %s\n", file_name);
		return -1;
	}
	size = stats.st_size;

	MD5Init(&context);
	if((input = fopen(file_name, "rb")) == NULL) {
		LM_ERR("could not open file %s\n", file_name);
		return -1;
	}

	while(size) {
		counter = (size > sizeof(buffer)) ? sizeof(buffer) : size;
		if ((counter = fread(buffer, 1, counter, input)) <= 0) {
			fclose(input);
			return -1;
		}
		U_MD5Update(&context, buffer, counter);
		size -= counter;
	}
	fclose(input);
	U_MD5Final(hash, &context);

	string2hex(hash, 16, dest);
	LM_DBG("MD5 calculated: %.*s for file %s\n", MD5_LEN, dest, file_name);

	return 0;
}
Пример #4
0
/* Digests a string array and store the result in dst; assumes
  32 bytes in dst
 */
void MD5StringArray (char *dst, str src[], int size)
{
	MD_CTX context;
	unsigned char digest[16];
 	int i;
	int len;
	char *s;

/*
#	ifdef EXTRA_DEBUG
	int j;
	int sum;
#endif
*/

	MDInit (&context);
	for (i=0; i<size; i++) {
		trim_len( len, s, src[i] );
/*
#		ifdef EXTRA_DEBUG
		fprintf(stderr, "EXTRA_DEBUG: %d. (%d) {", i+1, len);
		sum=0;
		for (j=0; j<len; j++) {
			fprintf( stderr, "%c ", *(s+j));
			sum+=*(s+j);
		}
		for (j=0; j<len; j++) {
			fprintf( stderr, "%d ", *(s+j));
			sum+=*(s+j);
		}
		fprintf(stderr, " [%d]\n", sum );	
#		endif
*/
		if (len > 0)
  			MDUpdate (&context, s, len);
  }
  MDFinal (digest, &context);

  string2hex(digest, 16, dst );
  DBG("DEBUG: MD5 calculated: %.*s\n", MD5_LEN, dst );

}
Пример #5
0
/*!
  * \brief Calculate a MD5 digests over a string array
  * 
  * Calculate a MD5 digests over a string array and stores the result in the
  * destination char array. This function assumes 32 bytes in the destination
  * buffer.
  * \param dst destination
  * \param src string input array
  * \param size elements in the input array
  */
void MD5StringArray (char *dst, str src[], int size)
{
	MD5_CTX context;
	unsigned char digest[16];
 	int i;
	int len;
	char *s;

	MD5Init (&context);
	for (i=0; i<size; i++) {
		trim_len( len, s, src[i] );
		if (len > 0)
  			MD5Update (&context, s, len);
  }
  U_MD5Final (digest, &context);

  string2hex(digest, 16, dst );
  DBG("DEBUG: MD5 calculated: %.*s\n", MD5_LEN, dst );

}
Пример #6
0
int main(int argc, char *argv[])
{
	__u8 miso[MAX_LENGTH];
	__u8 mosi[MAX_LENGTH];
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)mosi,
		.rx_buf = (unsigned long)miso,
		.delay_usecs = 1,
		.speed_hz = 8000000,
		.bits_per_word = 8,
		.len = 1,
	};
	char *device_name = NULL;
	char *mosi_str = "FF";
	int opt_i = 0;
	int c;
	int fd;
	int ret;

	static struct option long_opts[] = {
		{ "device", required_argument, 0, 'd' },
		{ "length", required_argument, 0, 'l' },
		{ "mosi", required_argument, 0, 'm' },
		{ "speed", required_argument, 0, 's' },
		{ "help", no_argument, 0, '?' },
		{ 0, 0, 0, 0 },
	};

	while ((c = getopt_long(argc, argv, "d:l:m:s:?",
				long_opts, &opt_i)) != -1) {
		switch (c) {
		case 'd':
			device_name = optarg;
			break;
		case 'l':
			tr.len = MIN(atoi(optarg), MAX_LENGTH);
			break;
		case 'm':
			mosi_str = optarg;
			break;
		case 's':
			tr.speed_hz = atoi(optarg);
			break;
		case '?':
			print_usage();
			return 0;
		}
	}

	if (!device_name) {
		fprintf(stderr, "Missing required device argument.\n");
		print_usage();
		return -1;
	}

	fd = open(device_name, O_RDWR);
	if (fd == -1) {
		fprintf(stderr, "main: opening device file: %s: %s\n",
		       device_name, strerror(errno));
		return -1;
	}

	string2hex(mosi_str, mosi, tr.len);

	printf("Sending to %s at %ld Hz\n", device_name, tr.speed_hz);

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret == -1)
		fprintf(stderr, "main: ioctl SPI_IOC_MESSAGE: %s: %s\n",
			device_name, strerror(errno));
	else
		print_spi_transaction(miso, mosi, tr.len);

	close(fd);

	return ret;
}