added more options to HashBuilder and added extractor method

This commit is contained in:
Sebastian Sdorra
2012-02-17 21:26:15 +01:00
parent fa759e8502
commit 694bbbd9f0
11 changed files with 521 additions and 16 deletions

View File

@@ -39,6 +39,11 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
//~--- JDK imports ------------------------------------------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Sebastian Sdorra
@@ -54,6 +59,18 @@ public abstract class HashBuilderTestBase
*/
public abstract HashBuilder createHashBuilder();
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
protected abstract String getLable();
//~--- methods --------------------------------------------------------------
/**
* Method description
*
@@ -74,6 +91,30 @@ public abstract class HashBuilderTestBase
checkHash("hitcheker", hash, otherHash);
}
/**
* Method description
*
*/
@Test
public void testCreateLabledHash()
{
HashBuilder hashBuilder = createHashBuilder();
String hash = hashBuilder.enableLabel().setValue("hitcheker").toHexString();
System.out.println(hash);
checkHash("hitcheker", hash);
Pattern p = Pattern.compile("\\{([^\\}]+)\\}.*");
Matcher m = p.matcher(hash);
assertTrue(m.matches());
String lable = m.group(1);
assertNotNull(lable);
assertEquals(getLable(), lable);
}
/**
* Method description
*
@@ -134,6 +175,19 @@ public abstract class HashBuilderTestBase
checkHash("hitcheker", hash, otherHash);
}
/**
* Method description
*
*
* @param plain
* @param hash
*/
private void checkHash(String plain, String hash)
{
assertNotNull(hash);
assertThat(hash, not(equalTo(plain)));
}
/**
* Method description
*
@@ -144,8 +198,7 @@ public abstract class HashBuilderTestBase
*/
private void checkHash(String plain, String hash, String otherHash)
{
assertNotNull(hash);
assertThat(hash, not(equalTo("hitcheker")));
checkHash(plain, hash);
assertNotNull(otherHash);
assertThat(otherHash, not(equalTo("hitcheker")));
assertEquals(hash, otherHash);

View File

@@ -49,4 +49,18 @@ public class MD5HashBuilderTest extends HashBuilderTestBase
{
return new MD5HashBuilder();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected String getLable()
{
return MD5HashBuilder.DIGEST;
}
}

View File

@@ -0,0 +1,63 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Sebastian Sdorra
*/
public class MessageDigestHashBuilderTest
{
/**
* Method description
*
*/
@Test
public void testExtractor()
{
MessageDigestHashBuilder hashBuilder = new SHA1HashBuilder("hitcheker");
String hash =
hashBuilder.enableLabel().createSalt().appendSalt().toHexString();
assertNotNull(hash);
hashBuilder =
MessageDigestHashBuilder.createExtractor(hash).getHashBuilder(8);
assertEquals(hash, hashBuilder.setValue("hitcheker").toHexString());
}
}

View File

@@ -69,4 +69,18 @@ public class SHA1HashBuilderTest extends HashBuilderTestBase
assertEquals(hash, newHash);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected String getLable()
{
return SHA1HashBuilder.DIGEST;
}
}

View File

@@ -49,4 +49,18 @@ public class SHA512HashBuilderTest extends HashBuilderTestBase
{
return new SHA512HashBuilder();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected String getLable()
{
return SHA512HashBuilder.DIGEST;
}
}