package com.fuse.security;

/*
	FUSE Light Server - multiuser server application
	Copyright (C) 2000 Aapo Kyrola / Sulake Oy  Helsinki, Finland

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/



import java.util.*;
import java.security.*;

/** 
  * Very simple SecretKey-implementation. Everyone should create
  * their own.
  * @author Aapo Kyrola
  */
public class VerySimpleSecretKey implements SecretKey {

	long seed = System.currentTimeMillis();
	
	/**
	  * Returns a key which client has to solve
	  */
	public String generateKey() {
		Random rand = new Random((seed += System.currentTimeMillis() % 500));
		StringBuffer sb = new StringBuffer(200);
		for(int i=0; i<20; i++) {
			sb.append(Math.abs(rand.nextInt() % 1000));
			sb.append(" ");	
		}
		return sb.toString();
	}
	
	/**
	  * Solves a key and returns the answer
	  */
	public String decodeKey(String key) {
		String s;
		int x, i=0;
		long z = 0;
		StringTokenizer zt = new StringTokenizer(key, " ");
		while(zt.hasMoreTokens()) {
			i++;
			s = zt.nextToken();
			x = Integer.parseInt(s);
			z += (x+i%5);
		}
		System.out.println("z=" + z + "; i=" + i +"; res=" + 1000*Math.sin(z * 1.0));
		return new Integer((int) (1000*Math.sin(z * 1.0))).toString();
	}
	
	public static void main(String[] args) {
		VerySimpleSecretKey keyCode = new VerySimpleSecretKey();
		System.out.println("Start");
		for(int i=0; i<10; i++) {
			String key = keyCode.generateKey();
			String decryptKey = keyCode.decodeKey(key);
			System.out.println(key);
			System.out.println(decryptKey);
			System.out.println("");		
		}	
	}
}
