/*	GenesisChess, an Android chess application
	Copyright 2012, Justin Madru (justin.jdm64@gmail.com)

	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://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 com.chess.genesis.net;

import java.io.*;
import java.net.*;
import java.security.*;

final class Crypto
{
	private Crypto()
	{
	}

	private static String calcHash(final MessageDigest digst)
	{
		final byte[] shabytes = digst.digest();
		final StringBuilder buff = new StringBuilder();

		for (final byte shabyte : shabytes) {
			final String n = Integer.toHexString(shabyte & 0xff);
			if (n.length() < 2)
				buff.append('0');
			buff.append(n);
		}
		return buff.toString();
	}

	private static String Sha1Hash(final String str)
	{
	try {
		final MessageDigest digst = MessageDigest.getInstance("SHA-1");

		digst.update(str.getBytes());
		return calcHash(digst);
	} catch (final java.security.NoSuchAlgorithmException e) {
		throw new RuntimeException(e.getMessage(), e);
	}
	}

	public static String HashPasswd(final String str)
	{
		return Sha1Hash(Sha1Hash(str));
	}

	public static String LoginKey(final SocketClient socket, final String str) throws SocketException, IOException
	{
	try {
		final MessageDigest digst = MessageDigest.getInstance("SHA-1");

		digst.update(HashPasswd(str).getBytes());
		digst.update(socket.getHash().getBytes());

		return calcHash(digst);
	} catch (final java.security.NoSuchAlgorithmException e) {
		throw new RuntimeException(e.getMessage(), e);
	}
	}
}

@@ -689,24 +689,43 @@
 java.net.*;%0A
+import java.nio.*;%0A
 import java.
@@ -737,16 +737,23 @@
 ity.*;%0A%0A
+public 
 final cl
@@ -1337,38 +1337,511 @@
 atch (final 
-java.security.
+NoSuchAlgorithmException e) %7B%0A%09%09throw new RuntimeException(e.getMessage(), e);%0A%09%7D%0A%09%7D%0A%0A%09public static String RandomSha1Hash()%0A%09%7B%0A%09try %7B%0A%09%09final MessageDigest digst = MessageDigest.getInstance(%22SHA-1%22);%0A%09%09final SecureRandom random = new SecureRandom();%0A%09%09final byte%5B%5D buffer = new byte%5B512%5D;%0A%09%09final long time = System.nanoTime();%0A%0A%09%09random.nextBytes(buffer);%0A%09%09digst.update(buffer);%0A%09%09digst.update(ByteBuffer.allocate(8).putLong(time).array());%0A%0A%09%09return calcHash(digst);%0A%09%7D catch (final 
 NoSuchAlgori
@@ -2327,30 +2327,16 @@
  (final 
-java.security.
 NoSuchAl

add Crypto.RandomSha1Hash() to generate a random sha1 hash