File: shasum.c

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (62 lines) | stat: -rw-r--r-- 983 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sha2.h"

#define BUF_SIZE	16384

int main(int argc, char *argv[])
{	FILE			*inf;
	sha256_ctx		ctx[1];
	unsigned char	buf[BUF_SIZE], hval[SHA256_DIGEST_SIZE];
	int				i, len, is_console;

	if(argc != 2)
	{
		printf("\nusage: shasum filename\n");
		exit(0);
	}

	if(is_console = (!strcmp(argv[1], "con") || !strcmp(argv[1], "CON")))
	{
		if(!(inf = fopen(argv[1], "r")))
		{
			printf("\n%s not found\n", argv[1]);
			exit(0);
		}
	}
	else if(!(inf = fopen(argv[1], "rb")))
	{
		printf("\n%s not found\n", argv[1]);
		exit(0);
	}

	sha256_begin(ctx);
	do
	{
		len = (int)fread(buf, 1, BUF_SIZE, inf);
		i = len;
		if(is_console)
		{
			i = 0;
			while(i < len && buf[i] != '\x1a')
				++i;
		}
		if(i)
			sha256_hash(buf, i, ctx);
	}
	while
		(len && i == len);

	fclose(inf);
	sha256_end(hval, ctx);

	printf("\n");
	for(i = 0; i < SHA256_DIGEST_SIZE; ++i)
		printf("%02x", hval[i]);
	printf("\n");

	return 0;
}