Class: Nostr::Crypto
- Inherits:
-
Object
- Object
- Nostr::Crypto
- Defined in:
- lib/nostr/crypto.rb
Overview
Performs cryptographic operations.
Constant Summary collapse
- BN_BASE =
Numeric base of the OpenSSL big number used in an event content’s encryption.
16
- CIPHER_CURVE =
Name of the cipher curve used in an event content’s encryption.
'secp256k1'
- CIPHER_ALGORITHM =
Name of the cipher algorithm used in an event content’s encryption.
'aes-256-cbc'
Instance Method Summary collapse
-
#check_sig!(message, public_key, signature) ⇒ Boolean
Verifies the given Signature and raises an
Schnorr::InvalidSignatureError
if it is invalid. -
#decrypt_text(recipient_private_key, sender_public_key, encrypted_text) ⇒ String
Decrypts a piece of text.
-
#encrypt_text(sender_private_key, recipient_public_key, plain_text) ⇒ String
Encrypts a piece of text.
-
#sign_event(event, private_key) ⇒ Event
Uses the private key to generate an event id and sign the event.
-
#sign_message(message, private_key) ⇒ Signature
Signs a message using the Schnorr signature algorithm.
-
#valid_sig?(message, public_key, signature) ⇒ Boolean
Verifies the given Signature and returns true if it is valid.
Instance Method Details
#check_sig!(message, public_key, signature) ⇒ Boolean
Verifies the given Signature and raises an Schnorr::InvalidSignatureError
if it is invalid
175 176 177 178 |
# File 'lib/nostr/crypto.rb', line 175 def check_sig!(, public_key, signature) signature = Schnorr::Signature.decode([signature].pack('H*')) Schnorr.check_sig!([].pack('H*'), [public_key].pack('H*'), signature.encode) end |
#decrypt_text(recipient_private_key, sender_public_key, encrypted_text) ⇒ String
Decrypts a piece of text
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nostr/crypto.rb', line 63 def decrypt_text(recipient_private_key, sender_public_key, encrypted_text) base64_encoded_text, iv = encrypted_text.split('?iv=') # Ensure iv and base64_encoded_text are not nil return '' unless iv && base64_encoded_text cipher = OpenSSL::Cipher.new(CIPHER_ALGORITHM).decrypt cipher.iv = Base64.decode64(iv) cipher.key = compute_shared_key(recipient_private_key, sender_public_key) plain_text = cipher.update(Base64.decode64(base64_encoded_text)) + cipher.final plain_text.force_encoding('UTF-8') end |
#encrypt_text(sender_private_key, recipient_public_key, plain_text) ⇒ String
Encrypts a piece of text
39 40 41 42 43 44 45 46 |
# File 'lib/nostr/crypto.rb', line 39 def encrypt_text(sender_private_key, recipient_public_key, plain_text) cipher = OpenSSL::Cipher.new(CIPHER_ALGORITHM).encrypt cipher.iv = iv = cipher.random_iv cipher.key = compute_shared_key(sender_private_key, recipient_public_key) encrypted_text = cipher.update(plain_text) + cipher.final encrypted_text = "#{Base64.encode64(encrypted_text)}?iv=#{Base64.encode64(iv)}" encrypted_text.gsub("\n", '') end |
#sign_event(event, private_key) ⇒ Event
Uses the private key to generate an event id and sign the event
91 92 93 94 95 96 97 98 99 |
# File 'lib/nostr/crypto.rb', line 91 def sign_event(event, private_key) event_digest = hash_event(event) signature = (event_digest, private_key) event.id = event_digest event.sig = signature event end |
#sign_message(message, private_key) ⇒ Signature
Signs a message using the Schnorr signature algorithm
118 119 120 121 122 123 124 |
# File 'lib/nostr/crypto.rb', line 118 def (, private_key) hex_private_key = Array(private_key).pack('H*') = Array().pack('H*') hex_signature = Schnorr.sign(, hex_private_key).encode.unpack1('H*') Signature.new(hex_signature.to_s) end |
#valid_sig?(message, public_key, signature) ⇒ Boolean
Verifies the given Signature and returns true if it is valid
147 148 149 150 |
# File 'lib/nostr/crypto.rb', line 147 def valid_sig?(, public_key, signature) signature = Schnorr::Signature.decode([signature].pack('H*')) Schnorr.valid_sig?([].pack('H*'), [public_key].pack('H*'), signature.encode) end |