2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
6
* This program is distributed in the hope that it will be useful,
7
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
* GNU General Public License for more details.
10
* You should have received a copy of the GNU General Public License
11
* along with this program; if not, write to the Free Software
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
package games.strategy.engine.random;
17
import games.strategy.engine.data.PlayerID;
18
import games.strategy.engine.framework.*;
19
import games.strategy.engine.vault.*;
22
* A random source that generates numbers using a secure algorithm shared
23
* between two players.
25
* Code originally contributed by Ben Giddings.
28
public class CryptoRandomSource implements IRandomSource
31
private final IRandomSource m_plainRandom = new PlainRandomSource();
34
* converts an int[] to a bytep[
36
public static byte[] intsToBytes(int[] ints)
38
byte[] rVal = new byte[ints.length * 4];
40
for (int i = 0; i < ints.length; i++)
42
rVal[4 * i] = (byte) (0x000000FF & ints[i]);
43
rVal[(4 * i) + 1] = (byte) ((0x000000FF & (ints[i] >> 8)));
44
rVal[(4 * i) + 2] = (byte) ((0x000000FF & (ints[i] >> 16)));
45
rVal[(4 * i) + 3] = (byte) ((0x000000FF & (ints[i] >> 24)));
51
static int byteToIntUnsigned(byte val)
56
public static int[] bytesToInts(byte[] bytes)
58
int[] rVal = new int[bytes.length / 4];
59
for (int i = 0; i < rVal.length; i++)
61
rVal[i] = byteToIntUnsigned(bytes[4*i]) +
62
(byteToIntUnsigned(bytes[4*i + 1]) << 8) +
63
(byteToIntUnsigned(bytes[4*i + 2]) << 16) +
64
(byteToIntUnsigned(bytes[4*i + 3]) << 24);
70
public static int[] xor(int[] val1, int[] val2, int max)
72
if (val1.length != val2.length)
74
throw new IllegalArgumentException("Arrays not of same length");
76
int[] rVal = new int[val1.length];
77
for (int i = 0; i < val1.length; i++)
79
rVal[i] = (val1[i] + val2[i]) % max;
86
//the remote players who involved in rolling the dice
87
//dice are rolled securly between us and her
88
final private PlayerID m_remotePlayer;
90
final private IGame m_game;
92
public CryptoRandomSource(PlayerID remotePlayer, IGame game)
94
m_remotePlayer = remotePlayer;
100
* All delegates should use random data that comes from both players so that
101
* neither player cheats.
103
public int getRandom(int max, String annotation)
105
return getRandom(max, 1, annotation)[0];
109
* Delegates should not use random data that comes from any other source.
111
public int[] getRandom(int max, int count, String annotation)
114
throw new IllegalArgumentException("Invalid count:" + count);
116
Vault vault = m_game.getVault();
118
//generate numbers locally, and put them in the vault
119
int[] localRandom = m_plainRandom.getRandom(max, count, annotation);
121
//lock it so the client knows that its there, but cant read it
122
VaultID localID = vault.lock(intsToBytes(localRandom));
124
//ask the remote to generate numbers
125
IRemoteRandom remote = (IRemoteRandom) (m_game.getRemoteMessenger().getRemote(ServerGame.getRemoteRandomName(m_remotePlayer)));
126
int[] remoteNumbers = remote.generate(max, count, annotation, localID);
129
//unlock ours, tell the client he can verify
130
vault.unlock(localID);
131
remote.verifyNumbers();
133
//finally, we join the two together to get the real value
134
return xor(localRandom, remoteNumbers, max);
b'\\ No newline at end of file'