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.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/crypto.rb', line 5

def initialize(client, device, storage_folder, storage_key)
  @client = client
  @device = device
  @storage_key = storage_key
  @storage_folder = storage_folder

  if File.exist?('account.pickle')
    # 1a) if alice's account file exists load the pickle from the file
    @account = SelfCrypto::Account.from_pickle(File.read('account.pickle'), @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/
    @client.post("/v1/apps/#{@client.jwt.id}/devices/#{@device}/pre_keys", keys)

    # 1b-v) store the account to a file
    File.write('account.pickle', @account.to_pickle(storage_key))
  end
end

Instance Method Details

#decrypt(message, sender, sender_device) ⇒ Object


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

def decrypt(message, sender, sender_device)
  session_file_name = "#{sender}:#{sender_device}-session.pickle"

  if File.exist?(session_file_name)
    # 7a) if carol's session file exists load the pickle from the file
    session_with_bob = SelfCrypto::Session.from_pickle(File.read(session_file_name), @storage_key)
  else
    # 7b-i) if you have not previously sent or received a message to/from bob,
    #       you should extract the initial message from the group message intended
    #       for your account id.
    m = SelfCrypto::GroupMessage.new(message.to_s).get_message("#{@client.jwt.id}:#{@device}")

    # 7b-ii) use the initial message to create a session for bob or carol
    session_with_bob = @account.inbound_session(m)

    # 7b-iii) store the session to a file
    File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
  end

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

  # 9) add all recipients and their sessions
  gs.add_participant("#{sender}:#{sender_device}", session_with_bob)

  # 10) decrypt the message ciphertext
  gs.decrypt("#{sender}:#{sender_device}", message).to_s
end

#encrypt(message, recipient, recipient_device) ⇒ Object


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

def encrypt(message, recipient, recipient_device)
  session_file_name = "#{recipient}:#{recipient_device}-session.pickle"

  if File.exist?(session_file_name)
    # 2a) if bob's session file exists load the pickle from the file
    session_with_bob = SelfCrypto::Session.from_pickle(File.read(session_file_name), @storage_key)
  else
    # 2b-i) if you have not previously sent or recevied a message to/from bob,
    #       you must get his identity key from GET /v1/identities/bob/
    ed25519_identity_key = @client.device_public_key(recipient, recipient_device)

    # 2b-ii) get a one time key for bob
    res = @client.get("/v1/identities/#{recipient}/devices/#{recipient_device}/pre_keys")

    if res.code != 200
      Selfid.logger.error "identity response : #{res.body[:message]}"
      raise "could not get identity pre_keys"
    end

    one_time_key = JSON.parse(res.body)["key"]

    # 2b-iii) convert bobs ed25519 identity key to a curve25519 key
    curve25519_identity_key = SelfCrypto::Util.ed25519_pk_to_curve25519(ed25519_identity_key.raw_public_key)

    # 2b-iv) create the session with bob
    session_with_bob = @account.outbound_session(curve25519_identity_key, one_time_key)

    # 2b-v) store the session to a file
    File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
  end

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

  # 4) add all recipients and their sessions
  gs.add_participant("#{recipient}:#{recipient_device}", session_with_bob)

  # 5) encrypt a message
  gs.encrypt(message).to_s
end