Class: SelfSDK::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, device, storage, storage_key) ⇒ Crypto

Returns a new instance of Crypto.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/crypto.rb', line 8

def initialize(client, device, storage, storage_key)
  @client = client
  @device = device
  @storage_key = storage_key
  @lock_strategy = true
  @mode = "r+"
  @storage = storage
  @keys = {}
  @mutex = Mutex.new

  @storage.tx do
    ::SelfSDK.logger.debug('Checking if account exists...')
    if @storage.
      # 1a) if alice's account file exists load the pickle from the file
      @account = SelfCrypto::Account.from_pickle(@storage., @storage_key)
    else
      # 1b-i) if create a new account for alice if one doesn't exist already
      @account = SelfCrypto::Account.from_seed(@client.jwt.key)

      # 1b-ii) generate some keys for alice and publish them
      @account.gen_otk(100)

      # 1b-iii) convert those keys to json
      @keys = @account.otk['curve25519'].map{|k,v| {id: k, key: v}}
      keys = @keys.to_json

      # 1b-iv) post those keys to POST /v1/identities/<selfid>/devices/1/pre_keys/
      res = @client.post("/v1/apps/#{@client.jwt.id}/devices/#{@device}/pre_keys", keys)
      raise 'unable to push prekeys, please try in a few minutes' if res.code != 200

      # 1b-v) store the account to a file
      @storage.(@account.to_pickle(@storage_key))
    end
  end
end

Instance Method Details

#decrypt(message, sender, sender_device, offset) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/crypto.rb', line 83

def decrypt(message, sender, sender_device, offset)
  ::SelfSDK.logger.debug("- [crypto] decrypting a message #{message}")
  sid = @storage.sid(sender, sender_device)

  pt = ""
  tx do
    ::SelfSDK.logger.debug("- [crypto] loading sessions for #{sid}")
    ::SelfSDK.logger.debug("- [crypto] #{@account.one_time_keys["curve25519"]}")
    session_with_bob = get_inbound_session_with_bob(@storage.session_get_olm(sid), message)

    # 8) create a group session and set the identity of the account you're using
    ::SelfSDK.logger.debug("- [crypto] create a group session and set the identity of the account #{@client.jwt.id}:#{@device}")
    gs = SelfCrypto::GroupSession.new("#{@client.jwt.id}:#{@device}")

    # 9) add all recipients and their sessions
    ::SelfSDK.logger.debug("- [crypto] add all recipients and their sessions #{sender}:#{sender_device}")
    gs.add_participant("#{sender}:#{sender_device}", session_with_bob)

    # 10) decrypt the message ciphertext
    ::SelfSDK.logger.debug("- [crypto] decrypt the message ciphertext")
    pt = gs.decrypt("#{sender}:#{sender_device}", message).to_s

    # 11) store the session to a file
    ::SelfSDK.logger.debug("- [crypto] store the session to a file")

    pickle = session_with_bob.to_pickle(@storage_key)
    @storage.session_update(sid, pickle)
    @storage.(offset)
  end
  pt
rescue => e
  ::SelfSDK.logger.debug("- [crypto] ERROR DECRYPTING: original message:  #{message}")
  ::SelfSDK.logger.debug("- [crypto] ERROR DECRYPTING: exception message: #{e.message}")
  ::SelfSDK.logger.debug("- [crypto] ERROR DECRYPTING: exception backtrace: #{e.backtrace}")
  @storage.(offset)
  pt
end

#encrypt(message, recipients) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/crypto.rb', line 44

def encrypt(message, recipients)
  ::SelfSDK.logger.debug('- [crypto] encrypting a message')

  # create a group session and set the identity of the account youre using
  ::SelfSDK.logger.debug('- [crypto] create a group session and set the identity of the account youre using')
  gs = SelfCrypto::GroupSession.new("#{@client.jwt.id}:#{@device}")

  sessions = {}
  ct = ""
  tx do
    gs = SelfCrypto::GroupSession.new("#{@client.jwt.id}:#{@device}")
    recipients.each do |r|
      sid = @storage.sid(r[:id], r[:device_id])
      next if sid == @storage.app_id

      session_with_bob = get_outbound_session_with_bob(@storage.session_get_olm(sid), r[:id], r[:device_id])
      ::SelfSDK.logger.debug("- [crypto]   adding group participant #{r[:id]}:#{r[:device_id]}")
      gs.add_participant("#{r[:id]}:#{r[:device_id]}", session_with_bob)
      sessions[sid] = session_with_bob
    rescue => e
      ::SelfSDK.logger.warn("- [crypto]   there is a problem adding group participant #{r[:id]}:#{r[:device_id]}, skipping...")
      ::SelfSDK.logger.warn("- [crypto] #{e}")
      next
    end

    # 5) encrypt a message
    ::SelfSDK.logger.debug("- [crypto] group encrypting message")
    ct = gs.encrypt(message).to_s

    # 6) store the session to a file
    ::SelfSDK.logger.debug("- [crypto] storing sessions")
    sessions.each do |sid, session_with_bob|
      pickle = session_with_bob.to_pickle(@storage_key)
      @storage.session_update(sid, pickle)
    end
  end
  ct
end