Class: SelfSDK::Crypto

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Crypto.



7
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
# File 'lib/crypto.rb', line 7

def initialize(client, device, storage_folder, storage_key)
  @client = client
  @device = device
  @storage_key = storage_key
  @storage_folder = "#{storage_folder}/#{@client.jwt.key_id}"

  if File.exist?()
    # 1a) if alice's account file exists load the pickle from the file
    @account = SelfCrypto::Account.from_pickle(File.read(), @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}}.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
    File.write(, @account.to_pickle(storage_key))
  end
end

Instance Method Details

#decrypt(message, sender, sender_device) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/crypto.rb', line 74

def decrypt(message, sender, sender_device)
  ::SelfSDK.logger.debug("decrypting a message")
  session_file_name = session_path(sender, sender_device)

  ::SelfSDK.logger.debug("loading sessions")
  session_with_bob = get_inbound_session_with_bob(message, session_file_name)

  # 8) create a group session and set the identity of the account you're using
  ::SelfSDK.logger.debug("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("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("decrypt the message ciphertext")
  pt = gs.decrypt("#{sender}:#{sender_device}", message).to_s

  # 11) store the session to a file
  ::SelfSDK.logger.debug("store the session to a file")
  File.write(session_file_name, session_with_bob.to_pickle(@storage_key))

  pt
end

#encrypt(message, recipients) ⇒ Object



35
36
37
38
39
40
41
42
43
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
# File 'lib/crypto.rb', line 35

def encrypt(message, recipients)
  ::SelfSDK.logger.debug('encrypting a message')

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

  sessions = {}
  ::SelfSDK.logger.debug('managing sessions with all recipients')
  recipients.each do |r|
    session_file_name = session_path(r[:id], r[:device_id])
    session_with_bob = nil

    begin
      session_with_bob = get_outbound_session_with_bob(r[:id], r[:device_id], session_file_name)
    rescue => e
      ::SelfSDK.logger.warn("  there is a problem adding group participant #{r[:id]}:#{r[:device_id]}, skipping...")
      ::SelfSDK.logger.warn(e)
      next
    end

    ::SelfSDK.logger.debug("  adding group participant #{r[:id]}:#{r[:device_id]}")
    gs.add_participant("#{r[:id]}:#{r[:device_id]}", session_with_bob)
    sessions[session_file_name] = session_with_bob
  end

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

  # 6) store the session to a file
  ::SelfSDK.logger.debug("storing sessions")
  sessions.each do |session_file_name, session_with_bob|
    File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
  end

  ct
end