2 #include <Rinternals.h>
6 * This code is adapted from the 'digest.c' code in the 'digest'
7 * package by Dirk Eddelbuettel <edd@debian.org> with contributions by
8 * Antoine Lucas, Jarek Tuszynski, Henrik Bengtsson and Simon Urbanek
11 SEXP
sha1_object(SEXP object
, SEXP skip_bytes
)
13 char output
[41]; /* SHA-1 is 40 bytes + '\0' */
17 unsigned char buffer
[20];
19 int nChar
= length(object
);
21 PROTECT(object
= coerceVector(object
, RAWSXP
));
23 PROTECT(skip_bytes
= coerceVector(skip_bytes
, INTSXP
));
24 skip
= INTEGER(skip_bytes
)[0];
35 sha1_update(&ctx
, (uint8
*) data
, nChar
);
36 sha1_finish(&ctx
, buffer
);
39 sprintf(output
+ i
* 2, "%02x", buffer
[i
]);
41 PROTECT(result
= allocVector(STRSXP
, 1));
42 SET_STRING_ELT(result
, 0, mkChar(output
));
49 SEXP
sha1_file(SEXP filename
, SEXP skip_bytes
)
51 char output
[41]; /* SHA-1 is 40 bytes + '\0' */
56 unsigned char buf
[1024];
57 unsigned char sha1sum
[20];
59 PROTECT(skip_bytes
= coerceVector(skip_bytes
, INTSXP
));
60 PROTECT(filename
= coerceVector(filename
, STRSXP
));
62 skip
= INTEGER(skip_bytes
)[0];
64 if(!(fp
= fopen(CHAR(STRING_ELT(filename
, 0)), "rb")))
65 error("unable to open input file");
67 fseek(fp
, skip
, SEEK_SET
);
70 while((nChar
= fread(buf
, 1, sizeof(buf
), fp
)) > 0)
71 sha1_update(&ctx
, buf
, nChar
);
74 sha1_finish(&ctx
, sha1sum
);
77 sprintf(output
+ i
* 2, "%02x", sha1sum
[i
]);
79 PROTECT(result
= allocVector(STRSXP
, 1));
80 SET_STRING_ELT(result
, 0, mkChar(output
));