Skip to content

Commit

Permalink
bugfix: BaseMac would not use bsize in certain cases
Browse files Browse the repository at this point in the history
The implementation of BaseMac would only take the bsize (size of the
hash) into account if the #doFinal(byte[], int) method was called.
Both other #doFinal methods would behave as if bsize==defbsize and
not cut the hash to the right size.
  • Loading branch information
Boris-de committed Jan 11, 2015
1 parent 665cbf0 commit 73de5b7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/main/java/net/schmizz/sshj/transport/mac/BaseMAC.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public BaseMAC(String algorithm, int bsize, int defbsize) {

@Override
public byte[] doFinal() {
return mac.doFinal();
return resizeToHashSize(mac.doFinal());
}

@Override
public byte[] doFinal(byte[] input) {
return mac.doFinal(input);
return resizeToHashSize(mac.doFinal(input));
}

@Override
Expand All @@ -62,6 +62,15 @@ public void doFinal(byte[] buf, int offset) {
}
}

private byte[] resizeToHashSize(byte[] buf) {
if (bsize == defbsize)
return buf;

byte[] result = new byte[bsize];
System.arraycopy(buf, 0, result, 0, bsize);
return result;
}

@Override
public int getBlockSize() {
return bsize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ public class HMACMD596Test {
private static final String EXPECTED_HMAC = "dff33c507463f9cf088a5ce8";

@Test
@Ignore
public void testUpdateWithDoFinal() {
HMACMD596 hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
@Ignore
public void testDoFinalWithInput() {
HMACMD596 hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ public class HMACSHA196Test {
private static final String EXPECTED_HMAC = "24ddeed57ad91465c5b59dce";

@Test
@Ignore
public void testUpdateWithDoFinal() {
HMACSHA196 hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
@Ignore
public void testDoFinalWithInput() {
HMACSHA196 hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
Expand Down

0 comments on commit 73de5b7

Please sign in to comment.