Location: UserService.login Example Data

Discussion: Test Data is matchingReported This is a featured thread

Showing 4 posts

cpatni
Test Data is matching
May 14 2008, 11:11 PM EDT | Post edited: May 14 2008, 11:11 PM EDT
I am getting df3490817aa209bb30bd27a8c0dd when I ran this test case.

@Test
public void testSignatureHMac() throws DatatypeConfigurationException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
String developerKey = "1f7d14a2db95e2f1d150207ddef31e329e02941a";
String userId = "testuser";
Date time = Wetpaint.xmlDate("2008-05-02T12:00:00.000-08:00");
String secret = "f2326ecc6ae62ad118a23829b3a979eeef3d3dc5";
int timestamp = 1209758400;
Assert.assertEquals(time.getTime() /1000, 1209758400);
String s = developerKey + userId + timestamp;
SecretKeySpec sks = new SecretKeySpec(Strings.hex(secret), "HmacSHA1");
Mac mac = Mac.getInstance(sks.getAlgorithm());
mac.init(sks);
String sig = Strings.hex(mac.doFinal(s.getBytes("UTF-8")));
Assert.assertEquals("d2d6f6516bffe756557ca4cb77a0304d684f5fc8", sig);
}

public static Date xmlDate(String text) throws DatatypeConfigurationException {
XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(text);
return xgc.toGregorianCalendar().getTime();
}
Do you find this valuable?    
Keyword tags: XML:KEY:DATATYPE:

cpatni
1. RE: Test Data is Not matching
May 14 2008, 11:12 PM EDT | Post edited: May 14 2008, 11:12 PM EDT
I meant that the test data is not matching. Do you find this valuable?    
ryan_wetpaint
ryan_wetpaint
2. RE: Test Data is Not matching
May 15 2008, 12:15 PM EDT | Post edited: May 15 2008, 12:15 PM EDT
Per your email, this has been resolved:

OK the problem is resolved. The bytes uses for initializing HmacSHA1 are secret.getBytes("UTF-8") instead of Hex.toBytes(secret). Secret string itself are bytes in hex format. Using the UTF-8 encoded bytes of hex string seems quite counterintuitive and perhaps happened by accident. Not by design.


Do you find this valuable?    
blake
blake
3. RE: Test Data is Not matching
May 15 2008, 1:52 PM EDT | Post edited: May 15 2008, 1:52 PM EDT
That's true, you should call String.getBytes("UTF-8") when constructing your SecretKeySpec and when calling Mac.doFinal(). It's intended that your code treat the secret as an opaque string rather than as hex-encoded bytes. Do you find this valuable?