1\name{hmac}
2\alias{hmac}
3\title{compute a hash-based message authentication code}
4\description{
5  The \code{hmac} function calculates a message authentication code
6  (MAC) involving the specified cryptographic hash function in
7  combination with a given secret key.
8}
9\usage{
10hmac(key, object,
11     algo = c("md5", "sha1", "crc32", "sha256", "sha512"),
12     serialize = FALSE, raw = FALSE, ...)
13}
14\arguments{
15  \item{key}{An arbitrary character or numeric vector, to use as
16    pre-shared secret key.}
17  \item{object}{An arbitrary R object which will then be passed to the
18    \code{\link{serialize}} function, unless the \code{serialize}
19    argument is set to \code{FALSE}.}
20  \item{algo}{The algorithms to be used; currently available choices are
21    \code{md5}, which is also the default, \code{sha1}, \code{crc32} and
22    \code{sha256}.}
23  \item{serialize}{default value of \code{serialize} is here FALSE, not
24    TRUE as it is in \code{digest}.}
25  \item{raw}{This flag alters the type of the output.  Setting this to
26    \code{TRUE} causes the function to return an object of type
27    \code{"raw"} instead of \code{"character"}.}
28  \item{...}{All remaining arguments are passed to \code{digest}.  }
29}
30\value{
31  The \code{hmac} function uses the \code{digest} to return a hash
32  digest as specified in the RFC 2104.
33}
34\references{
35  MD5: \url{https://www.ietf.org/rfc/rfc1321.txt}.
36
37  SHA-1: \url{https://en.wikipedia.org/wiki/SHA-1}.
38  SHA-256: \url{https://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf}.
39  CRC32:  The original reference webpage at \code{rocksoft.com} has
40  vanished from the web; see
41  \url{https://en.wikipedia.org/wiki/Cyclic_redundancy_check} for
42  general information on CRC algorithms.
43
44  \url{https://aarongifford.com/computers/sha.html} for the
45  integrated C implementation of sha-512.
46
47  The page for the code underlying the C functions used here for sha-1
48  and md5, and further references, is no longer accessible.  Please see
49  \url{https://en.wikipedia.org/wiki/SHA-1} and
50  \url{https://en.wikipedia.org/wiki/MD5}.
51
52  \url{https://zlib.net} for documentation on the zlib library which
53  supplied the code for crc32.
54
55  \url{https://en.wikipedia.org/wiki/SHA_hash_functions} for
56  documentation on the sha functions.
57}
58\author{Mario Frasca \email{mfrasca@zonnet.nl}.}
59\seealso{\code{\link{digest}}}
60
61\examples{
62
63
64
65## Standard RFC 2104 test vectors
66current <- hmac('Jefe', 'what do ya want for nothing?', "md5")
67target <- '750c783e6ab0b503eaa86e310a5db738'
68stopifnot(identical(target, as.character(current)))
69
70current <- hmac(rep(0x0b, 16), 'Hi There', "md5")
71target <- '9294727a3638bb1c13f48ef8158bfc9d'
72stopifnot(identical(target, as.character(current)))
73
74current <- hmac(rep(0xaa, 16), rep(0xdd, 50), "md5")
75target <- '56be34521d144c88dbb8c733f0e8b3f6'
76stopifnot(identical(target, as.character(current)))
77
78## SHA1 tests inspired to the RFC 2104 and checked against the python
79## hmac implementation.
80current <- hmac('Jefe', 'what do ya want for nothing?', "sha1")
81target <- 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'
82stopifnot(identical(target, as.character(current)))
83
84current <- hmac(rep(0x0b, 16), 'Hi There', "sha1")
85target <- '675b0b3a1b4ddf4e124872da6c2f632bfed957e9'
86stopifnot(identical(target, as.character(current)))
87
88current <- hmac(rep(0xaa, 16), rep(0xdd, 50), "sha1")
89target <- 'd730594d167e35d5956fd8003d0db3d3f46dc7bb'
90stopifnot(identical(target, as.character(current)))
91
92}
93\keyword{misc}
94
95