Sample code for 30+ languages & platforms
SQL Server

Ed25519 Key Exchange (Shared Secret)

See more Ed25519 Examples

Demonstrates how to create an Ed25519 shared secret for use in key exchange.

Note: This example requires Chilkat v9.5.0.83 or greater.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- Alice's private key, a:
    --     77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a
    -- Alice's public key, X25519(a, 9):
    --     8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a
    -- Bob's private key, b:
    --     5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb
    -- Bob's public key, X25519(b, 9):
    --     de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f
    -- Their shared secret, K:
    --     4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742

    DECLARE @alicePrivKey nvarchar(4000)
    SELECT @alicePrivKey = '77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a'
    DECLARE @bobPubKey nvarchar(4000)
    SELECT @bobPubKey = 'de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f'

    DECLARE @privKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- This example shows only one way of loading an Ed25519 private key.
    -- Chilkat can load other formats (JWK, PEM, ASN.1 DER, etc.)  
    -- You may do so by calling LoadAnyFormat or LoadAnyFormatFile.
    EXEC sp_OAMethod @privKey, 'LoadEd25519', @success OUT, @alicePrivKey, ''
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    DECLARE @pubKey int
    EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubKey OUT

    EXEC sp_OAMethod @pubKey, 'LoadEd25519', @success OUT, @bobPubKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pubKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @pubKey
        RETURN
      END

    -- Create the shared secret:
    DECLARE @eddsa int
    EXEC @hr = sp_OACreate 'Chilkat.EdDSA', @eddsa OUT

    DECLARE @sharedSecret nvarchar(4000)
    EXEC sp_OAMethod @eddsa, 'SharedSecretENC', @sharedSecret OUT, @privKey, @pubKey, 'hexlower'
    EXEC sp_OAGetProperty @eddsa, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @eddsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @pubKey
        EXEC @hr = sp_OADestroy @eddsa
        RETURN
      END


    PRINT 'Computed: ' + @sharedSecret

    PRINT 'Expected: 4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742'

    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @pubKey
    EXEC @hr = sp_OADestroy @eddsa


END
GO