]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/crypto/TestSPKAC.java
upd: rename package name and all references to it
[gigi.git] / tests / club / wpia / gigi / crypto / TestSPKAC.java
1 package club.wpia.gigi.crypto;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.security.GeneralSecurityException;
8 import java.security.KeyPair;
9 import java.security.KeyPairGenerator;
10 import java.security.Signature;
11 import java.security.SignatureException;
12 import java.security.interfaces.RSAKey;
13 import java.util.Base64;
14
15 import org.junit.Test;
16
17 import club.wpia.gigi.crypto.SPKAC;
18 import club.wpia.gigi.testUtils.IOUtils;
19 import sun.security.x509.X509Key;
20
21 public class TestSPKAC {
22
23     @Test
24     public void testParse() throws GeneralSecurityException, IOException {
25         String spkac = IOUtils.readURL(new InputStreamReader(TestSPKAC.class.getResourceAsStream("sampleSPKAC.txt"), "UTF-8"));
26         SPKAC parsed = new SPKAC(Base64.getDecoder().decode(spkac.replaceAll("[\r\n]", "")));
27         assertEquals("i am in the testcase", parsed.getChallenge());
28         RSAKey k = ((RSAKey) parsed.getPubkey());
29         assertEquals("a4004c2addf204fb26ce98f5963cc76def609ec0c50905e091fb84e986e3cb" + //
30                 "0d5e14edb9cb8e10524350bd2351589284a4f631ddf9b87f04ea0e58f7d8d816b58" + //
31                 "d052ce08b6576c04a7d45daf25b0ac9306f9cbb1f626e4ac301b7a4a3a062252b9a" + //
32                 "472b2cde5ec803407b18879a59ccba7716016b1de4537a005b2bd0fd6071", k.getModulus().toString(16));
33     }
34
35     @Test
36     public void testAddData() throws GeneralSecurityException, IOException {
37         String spkac = IOUtils.readURL(new InputStreamReader(TestSPKAC.class.getResourceAsStream("sampleSPKAC.txt"), "UTF-8"));
38         byte[] data = Base64.getDecoder().decode(spkac.replaceAll("[\r\n]", ""));
39         byte[] tampered = new byte[data.length + 1];
40         System.arraycopy(data, 0, tampered, 0, data.length);
41         try {
42             new SPKAC(tampered);
43             fail("Expected illegal arg exception.");
44         } catch (IllegalArgumentException e) {
45             // expected
46         }
47         // change the last byte of the signature.
48         data[data.length - 1]--;
49         try {
50             new SPKAC(data);
51             fail("Expected SignatureException.");
52         } catch (SignatureException e) {
53             // expected
54         }
55     }
56
57     @Test
58     public void testGen() throws GeneralSecurityException, IOException {
59         KeyPairGenerator pkg = KeyPairGenerator.getInstance("RSA");
60         pkg.initialize(1024);
61         KeyPair kp = pkg.generateKeyPair();
62
63         SPKAC s = new SPKAC((X509Key) kp.getPublic(), "this is a even bigger challange");
64         Signature sign = Signature.getInstance("SHA512withRSA");
65         sign.initSign(kp.getPrivate());
66
67         byte[] res = s.getEncoded(sign);
68         SPKAC parsed = new SPKAC(res);
69         assertEquals(s.getChallenge(), parsed.getChallenge());
70         assertEquals(s.getPubkey(), parsed.getPubkey());
71
72     }
73 }