View Javadoc
1   /*
2    * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.gpg.bc.internal.keys;
11  
12  import static org.junit.Assert.assertEquals;
13  
14  import java.math.BigInteger;
15  import java.util.Locale;
16  
17  import org.bouncycastle.openpgp.PGPException;
18  import org.bouncycastle.util.encoders.Hex;
19  import org.eclipse.jgit.util.sha1.SHA1;
20  import org.junit.Test;
21  
22  public class KeyGrip25519Test {
23  
24  	interface Hash {
25  		byte[] hash(SHA1 sha, BigInteger q) throws PGPException;
26  	}
27  
28  	private void assertKeyGrip(String key, String expectedKeyGrip, Hash hash)
29  			throws Exception {
30  		SHA1 grip = SHA1.newInstance();
31  		grip.setDetectCollision(false);
32  		BigInteger pk = new BigInteger(key, 16);
33  		byte[] keyGrip = hash.hash(grip, pk);
34  		assertEquals("Keygrip should match", expectedKeyGrip,
35  				Hex.toHexString(keyGrip).toUpperCase(Locale.ROOT));
36  	}
37  
38  	@Test
39  	public void testCompressed() throws Exception {
40  		assertKeyGrip("40"
41  				+ "773E72848C1FD5F9652B29E2E7AF79571A04990E96F2016BF4E0EC1890C2B7DB",
42  				"9DB6C64A38830F4960701789475520BE8C821F47",
43  				KeyGrip::hashEd25519);
44  	}
45  
46  	@Test
47  	public void testCompressedNoPrefix() throws Exception {
48  		assertKeyGrip(
49  				"773E72848C1FD5F9652B29E2E7AF79571A04990E96F2016BF4E0EC1890C2B7DB",
50  				"9DB6C64A38830F4960701789475520BE8C821F47",
51  				KeyGrip::hashEd25519);
52  	}
53  
54  	@Test
55  	public void testCurve25519() throws Exception {
56  		assertKeyGrip("40"
57  				+ "918C1733127F6BF2646FAE3D081A18AE77111C903B906310B077505EFFF12740",
58  				"0F89A565D3EA187CE839332398F5D480677DF49C",
59  				KeyGrip::hashCurve25519);
60  	}
61  }