Sha256: bae1eba48b9b367464208d5abd69085c137f9b5fbf467ff4f23b3e72414e8737

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Ed25519
  # Public key for verifying digital signatures
  class VerifyKey
    # Create a Ed25519::VerifyKey from its serialized Twisted Edwards representation
    #
    # @param key [String] 32-byte string representing a serialized public key
    def initialize(key)
      raise ArgumentError, "seed must be 32 bytes long" unless key.length == KEY_SIZE
      @key_bytes = key
    end

    # Verify an Ed25519 signature against the message
    #
    # @param signature [String] 64-byte string containing an Ed25519 signature
    # @param message [String] string containing message to be verified
    #
    # @raise Ed25519::VerifyError signature verification failed
    #
    # @return [true] message verified successfully
    def verify(signature, message)
      if signature.length != SIGNATURE_SIZE
        raise ArgumentError, "expected #{SIGNATURE_SIZE} byte signature, got #{signature.length}"
      end

      return true if Ed25519.provider.verify(@key_bytes, signature, message)
      raise VerifyError, "signature verification failed!"
    end

    # Return a compressed twisted Edwards coordinate representing the public key
    #
    # @return [String] bytestring serialization of this public key
    def to_bytes
      @key_bytes
    end
    alias to_str to_bytes

    # Show hex representation of serialized coordinate in string inspection
    def inspect
      "#<#{self.class}:#{@key_bytes.unpack('H*').first}>"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ed25519-1.1.0-jruby lib/ed25519/verify_key.rb
ed25519-1.1.0 lib/ed25519/verify_key.rb
ed25519-1.0.0-jruby lib/ed25519/verify_key.rb
ed25519-1.0.0 lib/ed25519/verify_key.rb