I'm trying to create an R script that will execute the HMAC algorithm for
key-hashing messages.  My hope is to use this script for some web
authentication via R.

The algorithm is found at http://www.ietf.org/rfc/rfc2104.txt

Here is some example code that I have done that does not work for HMAC-MD5.
I'm a big confused by the algorithm since I don't have a very good working
knowledge of digests or hex strings.   I would really appreciate some
assistance if anyone can take a look at this code and offer advice.

The RFC website does offer C code to execute the HMAC algorithm but I am not
a C expert.


################ HMAC Code #############

library(digest)

########## hex to binary

hexdat <- replicate(10, paste(format.hexmode(sample(16,4)-1),collapse=''))
bin <- apply(outer(0:15,3:0,function(x,y) x%/%(2^y)%%2),1,paste,collapse="")
names(bin) <- format.hexmode( 0:15 )

## test
## cbind( hexdat, sapply( strsplit(hexdat,'') , function(x) paste( bin[x],
collapse='' ) ) )


########## binary to hex

hex <- names(bin)
names(hex) <- bin



############ Test HMAC_MD5 ####################

### B=64
### L=16

#key <- "0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
##key_len = 16 bytes
data <- "Hi There"
##data_len = 8 bytes

## digest =  "0x9294727a3638bb1c13f48ef8158bfc9d"

ipad <- rep("36",64)
opad <- rep("5c",64)


k <- rep("0b", 16)
zeros <- rep("00",48)
K <- c(k, zeros)

K_bin <- cbind(K, sapply( strsplit(K,'') , function(x) paste( bin[x],
collapse='' ) ) )
ipad_bin <- cbind(ipad, sapply( strsplit(ipad,'') , function(x) paste(
bin[x], collapse='' ) ) )

#cbind(K_bin, sapply( strsplit(K_bin[,2],'') , as.numeric) )

K_binnum <- sapply( strsplit(K_bin[,2],'') , as.numeric)
ipad_binnum <- sapply( strsplit(ipad_bin[,2],'') , as.numeric)


InnerXOR <- as.numeric(xor(K_binnum, ipad_binnum))
InnerXOR <- matrix(InnerXOR, 4, 128)
InnerXOR <- t(InnerXOR)
InnerXORbin <- apply(InnerXOR, 1, paste, collapse='')

InnerXORhex <- cbind(InnerXORbin, sapply(InnerXORbin, function(x)
paste(hex[x], collapse='') ))
InnerXORhex <- t(matrix(InnerXORhex[,2],2,64))
InnerXORhex <- apply(InnerXORhex, 1, paste, collapse='')

dataHex <- charToRaw(data)

cmb <- c(InnerXORhex, dataHex)
cmb <- paste(cmb, collapse='')

InnerHash <- digest(cmb, algo="md5")

opad_bin <- cbind(opad, sapply( strsplit(opad,'') , function(x) paste(
bin[x], collapse='' ) ) )

opad_binnum <- sapply( strsplit(opad_bin[,2],'') , as.numeric)

OuterXOR <- as.numeric(xor(K_binnum, opad_binnum))
OuterXOR <- matrix(OuterXOR, 4, 128)
OuterXOR <- t(OuterXOR)
OuterXORbin <- apply(OuterXOR, 1, paste, collapse='')

OuterXORhex <- cbind(OuterXORbin, sapply(OuterXORbin, function(x)
paste(hex[x], collapse='') ))
OuterXORhex <- t(matrix(OuterXORhex[,2],2,64))
OuterXORhex <- apply(OuterXORhex, 1, paste, collapse='')
OuterXORhex <- paste(OuterXORhex, collapse='')

outercmb <- paste(OuterXORhex, InnerHash, sep="")
HMAC_MD5 <- digest(outercmb, algo="md5")    ######## should equal the digest
example above

	[[alternative HTML version deleted]]

