mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
added EncryptionHandler
This commit is contained in:
@@ -12,6 +12,8 @@ package sonia.scm;
|
|||||||
import sonia.scm.group.GroupManager;
|
import sonia.scm.group.GroupManager;
|
||||||
import sonia.scm.repository.BasicRepositoryManager;
|
import sonia.scm.repository.BasicRepositoryManager;
|
||||||
import sonia.scm.repository.RepositoryManager;
|
import sonia.scm.repository.RepositoryManager;
|
||||||
|
import sonia.scm.security.EncryptionHandler;
|
||||||
|
import sonia.scm.security.MessageDigestEncryptionHandler;
|
||||||
import sonia.scm.util.ServiceUtil;
|
import sonia.scm.util.ServiceUtil;
|
||||||
import sonia.scm.util.Util;
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
@@ -79,6 +81,7 @@ public class BasicContextProvider implements SCMContextProvider
|
|||||||
{
|
{
|
||||||
loadGroupManagers();
|
loadGroupManagers();
|
||||||
loadRepositoryManager();
|
loadRepositoryManager();
|
||||||
|
loadEncryptionHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
@@ -95,6 +98,18 @@ public class BasicContextProvider implements SCMContextProvider
|
|||||||
return baseDirectory;
|
return baseDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public EncryptionHandler getEncryptionHandler()
|
||||||
|
{
|
||||||
|
return encryptionHandler;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -155,6 +170,20 @@ public class BasicContextProvider implements SCMContextProvider
|
|||||||
return directory;
|
return directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void loadEncryptionHandler()
|
||||||
|
{
|
||||||
|
encryptionHandler = ServiceUtil.getService(EncryptionHandler.class);
|
||||||
|
|
||||||
|
if (encryptionHandler == null)
|
||||||
|
{
|
||||||
|
encryptionHandler = new MessageDigestEncryptionHandler();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -194,6 +223,9 @@ public class BasicContextProvider implements SCMContextProvider
|
|||||||
/** Field description */
|
/** Field description */
|
||||||
private File baseDirectory;
|
private File baseDirectory;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private EncryptionHandler encryptionHandler;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private Map<String, GroupManager> groupManagerMap;
|
private Map<String, GroupManager> groupManagerMap;
|
||||||
|
|
||||||
|
|||||||
@@ -11,15 +11,13 @@ package sonia.scm;
|
|||||||
|
|
||||||
import sonia.scm.group.GroupManager;
|
import sonia.scm.group.GroupManager;
|
||||||
import sonia.scm.repository.RepositoryManager;
|
import sonia.scm.repository.RepositoryManager;
|
||||||
import sonia.scm.repository.RepositoryType;
|
import sonia.scm.security.EncryptionHandler;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -43,6 +41,14 @@ public interface SCMContextProvider extends Closeable
|
|||||||
*/
|
*/
|
||||||
public File getBaseDirectory();
|
public File getBaseDirectory();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public EncryptionHandler getEncryptionHandler();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user