Sample code for 30+ languages & platforms
SQL Server

Load EC Public Key from X,Y Values

See more ECC Examples

Demonstrates how to load an EC public key from X and Y values.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- We have the following x and y values in base64 (for an EC point on the P-256 curve).
    DECLARE @x nvarchar(4000)
    SELECT @x = 'Dn7uB1O7kgk74G6qfQwFJESeDnxO6lLjGZFWZJE16tw'
    DECLARE @y nvarchar(4000)
    SELECT @y = 'iOWA5DInzK6nuUGvHJbMVq1Dpj248FqSV2teN3HzmhU'

    -- Build a JWK that looks like this:

    -- {
    --   "kty": "EC",
    --   "crv": "P-256",
    --   "x": "Dn7uB1O7kgk74G6qfQwFJESeDnxO6lLjGZFWZJE16tw",
    --   "y": "iOWA5DInzK6nuUGvHJbMVq1Dpj248FqSV2teN3HzmhU"
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'kty', 'EC'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'crv', 'P-256'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'x', @x
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'y', @y

    -- Load from the JWK.
    DECLARE @pubkey int
    EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubkey OUT

    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @pubkey, 'LoadFromString', @success OUT, @sTmp0
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pubkey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @pubkey
        RETURN
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @pubkey


END
GO