Author: erodriguez
Date: Tue Nov  9 19:43:39 2004
New Revision: 57127

Added:
   
incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/RandomKey.java
Log:
Consolidated all randkey generation in one place, removing it from 
CryptoService and elsewhere.

Added: 
incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/RandomKey.java
==============================================================================
--- (empty file)
+++ 
incubator/directory/kerberos/trunk/kerberos/src/java/org/apache/kerberos/crypto/RandomKey.java
      Tue Nov  9 19:43:39 2004
@@ -0,0 +1,59 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto;
+
+import org.apache.kerberos.crypto.encryption.EncryptionType;
+import org.apache.kerberos.messages.value.EncryptionKey;
+
+import javax.security.auth.kerberos.KerberosKey;
+import javax.security.auth.kerberos.KerberosPrincipal;
+import java.security.SecureRandom;
+
+public class RandomKey
+{
+    private static final SecureRandom random = new SecureRandom();
+
+       public EncryptionKey getNewSessionKey()
+    {
+               byte[] confounder = getRandomBytes( 8 );
+               DesStringToKey subSessionKey = new DesStringToKey(new 
String(confounder));
+
+               return new EncryptionKey(EncryptionType.DES_CBC_MD5, 
subSessionKey.getKey());
+       }
+
+    public static KerberosKey getRandomKeyFor(KerberosPrincipal principal)
+    {
+        final int DES_KEY_TYPE = 3;
+               int keyVersion = 1;
+
+               byte[] randomBytes = getRandomBytes( 8 );
+               DesStringToKey randomKey = new DesStringToKey( new String( 
randomBytes ) );
+
+               return new KerberosKey( principal, randomKey.getKey(), 
DES_KEY_TYPE, keyVersion );
+    }
+
+    private static byte[] getRandomBytes(int size)
+    {
+        byte[] bytes = new byte[size];
+
+        // SecureRandom.nextBytes is already synchronized
+        random.nextBytes(bytes);
+
+        return bytes;
+    }
+}
+

Reply via email to