You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KeyGrip25519Test.java 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. import static org.junit.Assert.assertEquals;
  12. import java.math.BigInteger;
  13. import java.util.Locale;
  14. import org.bouncycastle.openpgp.PGPException;
  15. import org.bouncycastle.util.encoders.Hex;
  16. import org.eclipse.jgit.util.sha1.SHA1;
  17. import org.junit.Test;
  18. public class KeyGrip25519Test {
  19. interface Hash {
  20. byte[] hash(SHA1 sha, BigInteger q) throws PGPException;
  21. }
  22. private void assertKeyGrip(String key, String expectedKeyGrip, Hash hash)
  23. throws Exception {
  24. SHA1 grip = SHA1.newInstance();
  25. grip.setDetectCollision(false);
  26. BigInteger pk = new BigInteger(key, 16);
  27. byte[] keyGrip = hash.hash(grip, pk);
  28. assertEquals("Keygrip should match", expectedKeyGrip,
  29. Hex.toHexString(keyGrip).toUpperCase(Locale.ROOT));
  30. }
  31. @Test
  32. public void testCompressed() throws Exception {
  33. assertKeyGrip("40"
  34. + "773E72848C1FD5F9652B29E2E7AF79571A04990E96F2016BF4E0EC1890C2B7DB",
  35. "9DB6C64A38830F4960701789475520BE8C821F47",
  36. KeyGrip::hashEd25519);
  37. }
  38. @Test
  39. public void testCompressedNoPrefix() throws Exception {
  40. assertKeyGrip(
  41. "773E72848C1FD5F9652B29E2E7AF79571A04990E96F2016BF4E0EC1890C2B7DB",
  42. "9DB6C64A38830F4960701789475520BE8C821F47",
  43. KeyGrip::hashEd25519);
  44. }
  45. @Test
  46. public void testCurve25519() throws Exception {
  47. assertKeyGrip("40"
  48. + "918C1733127F6BF2646FAE3D081A18AE77111C903B906310B077505EFFF12740",
  49. "0F89A565D3EA187CE839332398F5D480677DF49C",
  50. KeyGrip::hashCurve25519);
  51. }
  52. }