lib/universa/keys.rb in universa-3.10.5.3 vs lib/universa/keys.rb in universa-3.11.3.1
- old
+ new
@@ -30,10 +30,21 @@
# @return [PublicKey] public key that matches this
def public_key
@public_key ||= get_public_key
end
+
+ # @return key strength in bits, e.g. 2048, 4096...
+ def bit_strength
+ @public_key.bit_strength
+ end
+
+ # sign data or string with a specified hash type
+ # @return binary signature
+ def sign(data, hash_type = "SHA3_384")
+ __getobj__.sign(data.force_encoding('binary'), hash_type)
+ end
end
# A +com.icodici.crypto.PublicKey+ extension. As the key is immutable,
# caching is used to avoid unnecessary UMI calls.
class PublicKey < RemoteAdapter
@@ -50,19 +61,39 @@
else
PublicKey.new packed
end
end
+ # @return key strength in bits, e.g. 2048, 4096...
+ def bit_strength
+ getBitStrength()
+ end
+
# @return [KeyAddress] short address
def short_address
@short_address ||= get_short_address()
end
# @return [KeyAddress] long address
def long_address
@long_address ||= get_long_address()
end
+
+ # Check signature
+ # @param [String] data as binary or normal string
+ # @param [Object] signature as binary string
+ # @param [Object] hash_type to use
+ # @return true if it is ok
+ def verify(data, signature, hash_type = "SHA3_384")
+ __getobj__.verify(data.force_encoding('binary'), signature, hash_type)
+ end
+
+ # @param [String] data binary or usual data string
+ # @return [String] binary string with encrypted data
+ def encrypt(data)
+ __getobj__.encrypt(data.force_encoding('binary'))
+ end
end
# A +com.icodici.crypto.SymmetricKey+ extension. As the key is immutable,
# caching is used to avoid unnecessary UMI calls.
class SymmetricKey < RemoteAdapter
@@ -71,11 +102,11 @@
# Derive key from password using PBKDF2 standard
# @param [String] password to derive key from
# @param [Object] rounds derivation rounds
# @param [Object] salt optional salt used to disallow detect password match by key match
# @return [SymmetricKey] instance
- def self.from_password(password, rounds, salt=nil)
+ def self.from_password(password, rounds, salt = nil)
salt.force_encoding(Encoding::BINARY) if salt
invoke_static 'fromPassword', password, rounds, salt
end
# How many bits contains the key
@@ -91,9 +122,19 @@
# Get the key as binary string
# @return [String] key bytes (binary string)
def key
@key ||= getKey()
+ end
+
+ # Encrypt data using EtA (HMAC)
+ def eta_encrypt(plaintext)
+ __getobj__.etaEncrypt(plaintext.force_encoding('binary'))
+ end
+
+ # Decrypt data using EtA (HMAC)
+ def eta_decrypt(plaintext)
+ __getobj__.eta_decrypt(plaintext.force_encoding('binary'))
end
end
# The +com.icodici.crypto.KeyAddress+ extension. As it is immutable, caching is
# used to avoid unnecessary UMI calls.