From ebb08616c746ed2670aa7b1f614c5c8a5ff55cc8 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 20 Sep 2010 19:10:13 +0200 Subject: [PATCH] added EncryptionHandler --- .../java/sonia/scm/BasicContextProvider.java | 32 +++++ .../java/sonia/scm/SCMContextProvider.java | 12 +- .../scm/security/EncryptionException.java | 64 +++++++++ .../sonia/scm/security/EncryptionHandler.java | 26 ++++ .../MessageDigestEncryptionHandler.java | 133 ++++++++++++++++++ 5 files changed, 264 insertions(+), 3 deletions(-) create mode 100644 scm-core/src/main/java/sonia/scm/security/EncryptionException.java create mode 100644 scm-core/src/main/java/sonia/scm/security/EncryptionHandler.java create mode 100644 scm-core/src/main/java/sonia/scm/security/MessageDigestEncryptionHandler.java diff --git a/scm-core/src/main/java/sonia/scm/BasicContextProvider.java b/scm-core/src/main/java/sonia/scm/BasicContextProvider.java index d260778e1f..519989d304 100644 --- a/scm-core/src/main/java/sonia/scm/BasicContextProvider.java +++ b/scm-core/src/main/java/sonia/scm/BasicContextProvider.java @@ -12,6 +12,8 @@ package sonia.scm; import sonia.scm.group.GroupManager; import sonia.scm.repository.BasicRepositoryManager; import sonia.scm.repository.RepositoryManager; +import sonia.scm.security.EncryptionHandler; +import sonia.scm.security.MessageDigestEncryptionHandler; import sonia.scm.util.ServiceUtil; import sonia.scm.util.Util; @@ -79,6 +81,7 @@ public class BasicContextProvider implements SCMContextProvider { loadGroupManagers(); loadRepositoryManager(); + loadEncryptionHandler(); } //~--- get methods ---------------------------------------------------------- @@ -95,6 +98,18 @@ public class BasicContextProvider implements SCMContextProvider return baseDirectory; } + /** + * Method description + * + * + * @return + */ + @Override + public EncryptionHandler getEncryptionHandler() + { + return encryptionHandler; + } + /** * Method description * @@ -155,6 +170,20 @@ public class BasicContextProvider implements SCMContextProvider return directory; } + /** + * Method description + * + */ + private void loadEncryptionHandler() + { + encryptionHandler = ServiceUtil.getService(EncryptionHandler.class); + + if (encryptionHandler == null) + { + encryptionHandler = new MessageDigestEncryptionHandler(); + } + } + /** * Method description * @@ -194,6 +223,9 @@ public class BasicContextProvider implements SCMContextProvider /** Field description */ private File baseDirectory; + /** Field description */ + private EncryptionHandler encryptionHandler; + /** Field description */ private Map groupManagerMap; diff --git a/scm-core/src/main/java/sonia/scm/SCMContextProvider.java b/scm-core/src/main/java/sonia/scm/SCMContextProvider.java index bdb094048f..34b5f22fe6 100644 --- a/scm-core/src/main/java/sonia/scm/SCMContextProvider.java +++ b/scm-core/src/main/java/sonia/scm/SCMContextProvider.java @@ -11,15 +11,13 @@ package sonia.scm; import sonia.scm.group.GroupManager; import sonia.scm.repository.RepositoryManager; -import sonia.scm.repository.RepositoryType; +import sonia.scm.security.EncryptionHandler; //~--- JDK imports ------------------------------------------------------------ import java.io.Closeable; import java.io.File; -import java.util.Collection; - /** * * @author Sebastian Sdorra @@ -43,6 +41,14 @@ public interface SCMContextProvider extends Closeable */ public File getBaseDirectory(); + /** + * Method description + * + * + * @return + */ + public EncryptionHandler getEncryptionHandler(); + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/security/EncryptionException.java b/scm-core/src/main/java/sonia/scm/security/EncryptionException.java new file mode 100644 index 0000000000..a4800869e1 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/security/EncryptionException.java @@ -0,0 +1,64 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.security; + +/** + * + * @author Sebastian Sdorra + */ +public class EncryptionException extends RuntimeException +{ + + /** Field description */ + private static final long serialVersionUID = -3733681356044140444L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public EncryptionException() + { + super(); + } + + /** + * Constructs ... + * + * + * @param message + */ + public EncryptionException(String message) + { + super(message); + } + + /** + * Constructs ... + * + * + * @param cause + */ + public EncryptionException(Throwable cause) + { + super(cause); + } + + /** + * Constructs ... + * + * + * @param message + * @param cause + */ + public EncryptionException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/scm-core/src/main/java/sonia/scm/security/EncryptionHandler.java b/scm-core/src/main/java/sonia/scm/security/EncryptionHandler.java new file mode 100644 index 0000000000..9a85fd2d37 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/security/EncryptionHandler.java @@ -0,0 +1,26 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.security; + +/** + * + * @author Sebastian Sdorra + */ +public interface EncryptionHandler +{ + + /** + * Method description + * + * + * @param value + * + * @return + */ + public String encrypt(String value); +} diff --git a/scm-core/src/main/java/sonia/scm/security/MessageDigestEncryptionHandler.java b/scm-core/src/main/java/sonia/scm/security/MessageDigestEncryptionHandler.java new file mode 100644 index 0000000000..d6ac2d070e --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/security/MessageDigestEncryptionHandler.java @@ -0,0 +1,133 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.security; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.util.AssertUtil; + +//~--- JDK imports ------------------------------------------------------------ + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * + * @author Sebastian Sdorra + */ +public class MessageDigestEncryptionHandler implements EncryptionHandler +{ + + /** Field description */ + public static final String DEFAULT_DIGEST = "SHA-1"; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public MessageDigestEncryptionHandler() + { + this.digest = DEFAULT_DIGEST; + } + + /** + * Constructs ... + * + * + * @param digest + */ + public MessageDigestEncryptionHandler(String digest) + { + this.digest = digest; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param value + * + * @return + */ + @Override + public String encrypt(String value) + { + String result = null; + + try + { + AssertUtil.assertIsNotEmpty(value); + + MessageDigest messageDigest = MessageDigest.getInstance(digest); + + result = encrypt(messageDigest, value); + } + catch (NoSuchAlgorithmException ex) + { + throw new EncryptionException(ex); + } + + return result; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public String getDigest() + { + return digest; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param messageDigest + * @param value + * + * @return + */ + private String encrypt(MessageDigest messageDigest, String value) + { + messageDigest.reset(); + messageDigest.update(value.getBytes()); + + byte hashCode[] = messageDigest.digest(); + StringBuilder hashString = new StringBuilder(); + + for (int i = 0; i < hashCode.length; i++) + { + int x = hashCode[i] & 0xff; + + if (x < 16) + { + hashString.append('0'); + } + + hashString.append(Integer.toString(x, 16)); + } + + return hashString.toString(); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String digest; +}