|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.integrations.base.ssh; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.security.KeyPair; |
| 11 | +import java.security.PrivateKey; |
| 12 | +import java.security.PublicKey; |
| 13 | +import org.apache.sshd.common.util.security.SecurityUtils; |
| 14 | +import org.apache.sshd.common.util.security.eddsa.EdDSASecurityProviderRegistrar; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +class SshTunnelTest { |
| 18 | + |
| 19 | + /** |
| 20 | + * This test verifies that 'net.i2p.crypto:eddsa' is present and EdDSA is supported. If |
| 21 | + * net.i2p.crypto:eddsa will be removed from project, then will be thrown: generator not correctly |
| 22 | + * initialized |
| 23 | + * |
| 24 | + * @throws Exception |
| 25 | + */ |
| 26 | + @Test |
| 27 | + public void edDsaIsSupported() throws Exception { |
| 28 | + var keygen = SecurityUtils.getKeyPairGenerator("EdDSA"); |
| 29 | + final String message = "hello world"; |
| 30 | + KeyPair keyPair = keygen.generateKeyPair(); |
| 31 | + |
| 32 | + byte[] signedMessage = sign(keyPair.getPrivate(), message); |
| 33 | + |
| 34 | + assertTrue(new EdDSASecurityProviderRegistrar().isSupported()); |
| 35 | + assertTrue(verify(keyPair.getPublic(), signedMessage, message)); |
| 36 | + } |
| 37 | + |
| 38 | + private byte[] sign(final PrivateKey privateKey, final String message) throws Exception { |
| 39 | + var signature = SecurityUtils.getSignature("NONEwithEdDSA"); |
| 40 | + signature.initSign(privateKey); |
| 41 | + |
| 42 | + signature.update(message.getBytes(StandardCharsets.UTF_8)); |
| 43 | + |
| 44 | + return signature.sign(); |
| 45 | + } |
| 46 | + |
| 47 | + private boolean verify(final PublicKey publicKey, byte[] signed, final String message) |
| 48 | + throws Exception { |
| 49 | + var signature = SecurityUtils.getSignature("NONEwithEdDSA"); |
| 50 | + signature.initVerify(publicKey); |
| 51 | + |
| 52 | + signature.update(message.getBytes(StandardCharsets.UTF_8)); |
| 53 | + |
| 54 | + return signature.verify(signed); |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments