Hi Aleksey,

The rough C code used to verify
http://www.w3.org/TR/xmldsig-core/#sec-MessageDigests is attached.
This might prove useful for others.

Kind regards,
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char cs[] = {'0', '1', '2', '3','4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

int
main(int argc, char *argv[]) {
	char inalphabet[256], decoder[256];
	char result[256];
	int d1, d2, i, j, bits, c, char_count, errors = 0;

	for(i = (sizeof alphabet) - 1; i >= 0 ; i--) {
		inalphabet[alphabet[i]] = 1;
		decoder[alphabet[i]] = i;
	}

	char_count = 0;
	bits = 0;
	i = 0;
	while ((c = getchar()) != EOF) {
		if (c == '=')
			break;
		if (c > 255 || ! inalphabet[c])
			continue;
		bits += decoder[c];
		char_count++;
		if (char_count == 4) {
			result[i++] = (bits >> 16);
			result[i++] = ((bits >> 8) & 0xff);
			result[i++] = (bits & 0xff);
			bits = 0;
			char_count = 0;
		} else {
			bits <<= 6;
		}
	}
	if (c == EOF) {
		if (char_count) {
			fprintf(stderr, "base64 encoding incomplete: at least %d bits truncated",
					((4 - char_count) * 6));
			errors++;
		}
	}
	else { /* c == '=' */
		switch (char_count) {
			case 1:
				fprintf(stderr, "base64 encoding incomplete: at least 2 bits missing");
				errors++;
				break;
			case 2:
				result[i++] = (bits >> 10);
				break;
			case 3:
				result[i++] = (bits >> 16);
				result[i++] = ((bits >> 8) & 0xff);
				break;
		}
	}

	for(j = 0; j < i; j++) {
		putchar(cs[(result[j]>>4) & 0xf]);
		putchar(cs[(result[j]) & 0xf]);
	}
	putchar('\n');
	return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int
main(int argc, char *argv[]) {
	int d1, d2, cols, bits, c, char_count;
	char *p;

	if(argc != 2) {
		fprintf(stderr, "usage: %s <hex string>\n", argv[0]);
		exit(1);
	}
	for(p = argv[1]; *p; p++) {
		if(*p == '\0' || (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f'))
			;
		else {
			fprintf(stderr, "hash is not hexadecimal %s\n", p);
			exit(1);
		}
	}

	char_count = 0;
	bits = 0;
	cols = 0;
	for(p = argv[1]; *p; p++) {
		if(*p >= '0' && *p <= '9')
			d1 = *p - '0';
		else if(*p >= 'a' && *p <= 'f')
			d1 = *p - 'a' + 10;
		p++;
		if(*p >= '0' && *p <= '9')
			d2 = *p - '0';
		else if(*p >= 'a' && *p <= 'f')
			d2 = *p - 'a' + 10;
		c =  (d1 * 16) + d2;
		if(c > 255) {
			fprintf(stderr, "encountered char > 255 (decimal %d)", c);
			exit(1);
		}
		bits += c;
		char_count++;
		if (char_count == 3) {
			putchar(alphabet[bits >> 18]);
			putchar(alphabet[(bits >> 12) & 0x3f]);
			putchar(alphabet[(bits >> 6) & 0x3f]);
			putchar(alphabet[bits & 0x3f]);
			cols += 4;
			if (cols == 72) {
				putchar('\n');
				cols = 0;
			}
			bits = 0;
			char_count = 0;
		}
		else {
			bits <<= 8;
		}
	}
	if(char_count != 0) {
		bits <<= 16 - (8 * char_count);
		putchar(alphabet[bits >> 18]);
		putchar(alphabet[(bits >> 12) & 0x3f]);
		if (char_count == 1) {
			putchar('=');
			putchar('=');
		} else {
			putchar(alphabet[(bits >> 6) & 0x3f]);
			putchar('=');
		}
		if (cols > 0)
			putchar('\n');
	}

	return 0;
}
_______________________________________________
xmlsec mailing list
[email protected]
http://www.aleksey.com/mailman/listinfo/xmlsec

Reply via email to