starkbank-ecdsa 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ecdsa.rb +21 -17
- data/lib/privatekey.rb +36 -32
- data/lib/publickey.rb +27 -23
- data/lib/signature.rb +24 -21
- data/lib/starkbank-ecdsa.rb +0 -14
- data/lib/utils/file.rb +14 -6
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 5e63c03244cb43717832696cc0567614e40d1812791f6d1aef90627c4f01a054
         | 
| 4 | 
            +
              data.tar.gz: ac8cfa030831acaa46edd0fb8d0b34ed4b05230657042edf1b3c8bc19df04b83
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: cd480ea49374c8c573116f1b6283700cc1f6798b20aca5319b75d29f3cab67409532f95590d40a4846808e4a6ae78f4c8af7095efbe7fcbf83a1f21cb5a09a43
         | 
| 7 | 
            +
              data.tar.gz: c422e3a7453b412fe8718c7494256a7c0485f84556a76baa6062cc04917b50ec5e75cfae6a39b7baa20eab3b28faa605412444b21b892b2c8a7996917ba6251a
         | 
    
        data/lib/ecdsa.rb
    CHANGED
    
    | @@ -3,26 +3,30 @@ require 'openssl' | |
| 3 3 | 
             
            require 'signature'
         | 
| 4 4 |  | 
| 5 5 |  | 
| 6 | 
            -
            module  | 
| 6 | 
            +
            module EllipticCurve
         | 
| 7 7 |  | 
| 8 | 
            -
                 | 
| 9 | 
            -
                    if hashfunc.nil?
         | 
| 10 | 
            -
                        message = Digest::SHA256.digest(message)
         | 
| 11 | 
            -
                    else
         | 
| 12 | 
            -
                        message = hashfunc(message)
         | 
| 13 | 
            -
                    end
         | 
| 8 | 
            +
                module Ecdsa
         | 
| 14 9 |  | 
| 15 | 
            -
                     | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 10 | 
            +
                    def self.sign(message, privateKey, hashfunc=nil)
         | 
| 11 | 
            +
                        if hashfunc.nil?
         | 
| 12 | 
            +
                            message = Digest::SHA256.digest(message)
         | 
| 13 | 
            +
                        else
         | 
| 14 | 
            +
                            message = hashfunc(message)
         | 
| 15 | 
            +
                        end
         | 
| 18 16 |  | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
                        message = Digest::SHA256.digest(message)
         | 
| 22 | 
            -
                    else
         | 
| 23 | 
            -
                        message = hashfunc(message)
         | 
| 17 | 
            +
                        signature = privateKey.openSslPrivateKey.dsa_sign_asn1(message)
         | 
| 18 | 
            +
                        return Signature.new(signature)
         | 
| 24 19 | 
             
                    end
         | 
| 25 | 
            -
             | 
| 20 | 
            +
             | 
| 21 | 
            +
                    def self.verify(message, signature, publicKey, hashfunc=nil)
         | 
| 22 | 
            +
                        if hashfunc.nil?
         | 
| 23 | 
            +
                            message = Digest::SHA256.digest(message)
         | 
| 24 | 
            +
                        else
         | 
| 25 | 
            +
                            message = hashfunc(message)
         | 
| 26 | 
            +
                        end
         | 
| 27 | 
            +
                        return publicKey.openSslPublicKey.dsa_verify_asn1(message, signature.toDer())
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
             | 
| 26 30 | 
             
                end
         | 
| 27 31 |  | 
| 28 | 
            -
            end
         | 
| 32 | 
            +
            end
         | 
    
        data/lib/privatekey.rb
    CHANGED
    
    | @@ -3,47 +3,51 @@ require "base64" | |
| 3 3 | 
             
            require_relative "publickey"
         | 
| 4 4 |  | 
| 5 5 |  | 
| 6 | 
            -
             | 
| 6 | 
            +
            module EllipticCurve
         | 
| 7 7 |  | 
| 8 | 
            -
                 | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
                         | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 8 | 
            +
                class PrivateKey
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                    def initialize(curve="secp256k1", openSslKey=nil)
         | 
| 11 | 
            +
                        if openSslKey.nil?
         | 
| 12 | 
            +
                            @openSslPrivateKey = OpenSSL::PKey::EC.new(curve)
         | 
| 13 | 
            +
                            @openSslPrivateKey.generate_key
         | 
| 14 | 
            +
                        else
         | 
| 15 | 
            +
                            @openSslPrivateKey = openSslKey
         | 
| 16 | 
            +
                        end
         | 
| 14 17 | 
             
                    end
         | 
| 15 | 
            -
                end
         | 
| 16 18 |  | 
| 17 | 
            -
             | 
| 19 | 
            +
                    attr_reader :openSslPrivateKey
         | 
| 18 20 |  | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 21 | 
            +
                    def publicKey
         | 
| 22 | 
            +
                        dupKey = OpenSSL::PKey::EC.new(@openSslPrivateKey.to_der())
         | 
| 23 | 
            +
                        dupKey.private_key = nil
         | 
| 24 | 
            +
                        return PublicKey.new(dupKey)
         | 
| 25 | 
            +
                    end
         | 
| 24 26 |  | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 27 | 
            +
                    def toString
         | 
| 28 | 
            +
                        return Base64.encode64(self.toDer())
         | 
| 29 | 
            +
                    end
         | 
| 28 30 |  | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 31 | 
            +
                    def toDer
         | 
| 32 | 
            +
                        return @openSslPrivateKey.to_der()
         | 
| 33 | 
            +
                    end
         | 
| 32 34 |  | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 35 | 
            +
                    def toPem
         | 
| 36 | 
            +
                        return @openSslPrivateKey.to_pem()
         | 
| 37 | 
            +
                    end
         | 
| 36 38 |  | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 39 | 
            +
                    def self.fromPem(string)
         | 
| 40 | 
            +
                        return PrivateKey.new(nil, OpenSSL::PKey::EC.new(string))
         | 
| 41 | 
            +
                    end
         | 
| 40 42 |  | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 43 | 
            +
                    def self.fromDer(string)
         | 
| 44 | 
            +
                        return PrivateKey.new(nil, OpenSSL::PKey::EC.new(string))
         | 
| 45 | 
            +
                    end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                    def self.fromString(string)
         | 
| 48 | 
            +
                        return PrivateKey.new(nil, OpenSSL::PKey::EC.new(Base64.decode64(string)))
         | 
| 49 | 
            +
                    end
         | 
| 44 50 |  | 
| 45 | 
            -
                def self.fromString(string)
         | 
| 46 | 
            -
                    return PrivateKey.new(nil, OpenSSL::PKey::EC.new(Base64.decode64(string)))
         | 
| 47 51 | 
             
                end
         | 
| 48 52 |  | 
| 49 | 
            -
            end
         | 
| 53 | 
            +
            end
         | 
    
        data/lib/publickey.rb
    CHANGED
    
    | @@ -1,33 +1,37 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            module EllipticCurve
         | 
| 2 2 |  | 
| 3 | 
            -
                 | 
| 4 | 
            -
                    @openSslPublicKey = openSslPublicKey
         | 
| 5 | 
            -
                end
         | 
| 3 | 
            +
                class PublicKey
         | 
| 6 4 |  | 
| 7 | 
            -
             | 
| 5 | 
            +
                    def initialize(openSslPublicKey)
         | 
| 6 | 
            +
                        @openSslPublicKey = openSslPublicKey
         | 
| 7 | 
            +
                    end
         | 
| 8 8 |  | 
| 9 | 
            -
             | 
| 10 | 
            -
                    return Base64.encode64(self.toDer())
         | 
| 11 | 
            -
                end
         | 
| 9 | 
            +
                    attr_reader :openSslPublicKey
         | 
| 12 10 |  | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 11 | 
            +
                    def toString
         | 
| 12 | 
            +
                        return Base64.encode64(self.toDer())
         | 
| 13 | 
            +
                    end
         | 
| 16 14 |  | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 15 | 
            +
                    def toDer
         | 
| 16 | 
            +
                        @openSslPublicKey.to_der()
         | 
| 17 | 
            +
                    end
         | 
| 20 18 |  | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 19 | 
            +
                    def toPem
         | 
| 20 | 
            +
                        @openSslPublicKey.to_pem()
         | 
| 21 | 
            +
                    end
         | 
| 24 22 |  | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 23 | 
            +
                    def self.fromPem(string)
         | 
| 24 | 
            +
                        return PublicKey.new(OpenSSL::PKey::EC.new(string))
         | 
| 25 | 
            +
                    end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                    def self.fromDer(string)
         | 
| 28 | 
            +
                        return PublicKey.new(OpenSSL::PKey::EC.new(string))
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                    def self.fromString(string)
         | 
| 32 | 
            +
                        return PublicKey.new(OpenSSL::PKey::EC.new(Base64.decode64(string)))
         | 
| 33 | 
            +
                    end
         | 
| 28 34 |  | 
| 29 | 
            -
                def self.fromString(string)
         | 
| 30 | 
            -
                    return PublicKey.new(OpenSSL::PKey::EC.new(Base64.decode64(string)))
         | 
| 31 35 | 
             
                end
         | 
| 32 36 |  | 
| 33 | 
            -
            end
         | 
| 37 | 
            +
            end
         | 
    
        data/lib/signature.rb
    CHANGED
    
    | @@ -2,31 +2,34 @@ require "base64" | |
| 2 2 | 
             
            require "openssl"
         | 
| 3 3 |  | 
| 4 4 |  | 
| 5 | 
            -
             | 
| 5 | 
            +
            module EllipticCurve
         | 
| 6 6 |  | 
| 7 | 
            -
                 | 
| 8 | 
            -
                    @der = der
         | 
| 9 | 
            -
                    decoded = OpenSSL::ASN1.decode(der).value
         | 
| 10 | 
            -
                    @r = decoded[0].value
         | 
| 11 | 
            -
                    @s = decoded[1].value
         | 
| 12 | 
            -
                end
         | 
| 7 | 
            +
                class Signature
         | 
| 13 8 |  | 
| 14 | 
            -
             | 
| 9 | 
            +
                    def initialize(der)
         | 
| 10 | 
            +
                        @der = der
         | 
| 11 | 
            +
                        decoded = OpenSSL::ASN1.decode(der).value
         | 
| 12 | 
            +
                        @r = decoded[0].value
         | 
| 13 | 
            +
                        @s = decoded[1].value
         | 
| 14 | 
            +
                    end
         | 
| 15 15 |  | 
| 16 | 
            -
             | 
| 17 | 
            -
                    return @der
         | 
| 18 | 
            -
                end
         | 
| 16 | 
            +
                    attr_reader :r, :s
         | 
| 19 17 |  | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 18 | 
            +
                    def toDer
         | 
| 19 | 
            +
                        return @der
         | 
| 20 | 
            +
                    end
         | 
| 23 21 |  | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 22 | 
            +
                    def toBase64
         | 
| 23 | 
            +
                        Base64.encode64(self.toDer()).gsub("\n", "")
         | 
| 24 | 
            +
                    end
         | 
| 27 25 |  | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 26 | 
            +
                    def self.fromDer(string)
         | 
| 27 | 
            +
                        return Signature.new(string)
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                    def self.fromBase64(string)
         | 
| 31 | 
            +
                        self.fromDer(Base64.decode64(string))
         | 
| 32 | 
            +
                    end
         | 
| 31 33 |  | 
| 32 | 
            -
            end
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
            end
         | 
    
        data/lib/starkbank-ecdsa.rb
    CHANGED
    
    | @@ -3,17 +3,3 @@ require_relative "publickey" | |
| 3 3 | 
             
            require_relative "privatekey"
         | 
| 4 4 | 
             
            require_relative "ecdsa"
         | 
| 5 5 | 
             
            require_relative "utils/file"
         | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
            module EllipticCurve
         | 
| 9 | 
            -
             | 
| 10 | 
            -
                Signature = Signature
         | 
| 11 | 
            -
                PublicKey = PublicKey
         | 
| 12 | 
            -
                PrivateKey = PrivateKey
         | 
| 13 | 
            -
                Ecdsa = Ecdsa
         | 
| 14 | 
            -
             | 
| 15 | 
            -
                module Utils
         | 
| 16 | 
            -
                    File = File
         | 
| 17 | 
            -
                end
         | 
| 18 | 
            -
             | 
| 19 | 
            -
            end
         | 
    
        data/lib/utils/file.rb
    CHANGED
    
    | @@ -1,10 +1,18 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            module EllipticCurve
         | 
| 2 | 
            +
             | 
| 3 | 
            +
                module Utils
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                    class File
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                        def self.read(path, encoding="ASCII")
         | 
| 8 | 
            +
                            file = ::File.open(path, :encoding => encoding.upcase)
         | 
| 9 | 
            +
                            content = file.read
         | 
| 10 | 
            +
                            file.close
         | 
| 11 | 
            +
                            content
         | 
| 12 | 
            +
                        end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    end
         | 
| 2 15 |  | 
| 3 | 
            -
                def self.read(path, encoding="ASCII")
         | 
| 4 | 
            -
                    file = File.open(path, :encoding => encoding.upcase)
         | 
| 5 | 
            -
                    content = file.read
         | 
| 6 | 
            -
                    file.close
         | 
| 7 | 
            -
                    return content
         | 
| 8 16 | 
             
                end
         | 
| 9 17 |  | 
| 10 18 | 
             
            end
         |