ruby-safenet 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +38 -10
  3. data/lib/safenet.rb +105 -4
  4. data/lib/safenet/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d19872be0e8e5b424ad1d294cb7e1995ef94e5fb
4
- data.tar.gz: ee22a0a1939f51c5f5cca9286432511c5bb51035
3
+ metadata.gz: 974008fc8230e6155b425d997bb8a305dcd8537e
4
+ data.tar.gz: 1183766c5674d6f23291a8e2a4259b68a120c10e
5
5
  SHA512:
6
- metadata.gz: fd9425061291ce3644048530842316121962937b4413810d0bfca1f273623b2149b067a9439fe7fdff97893c8feee6f7a2361f77db0eac7bda46b314b77176ee
7
- data.tar.gz: 041aa3a0e7e08cf270ad3bb4e93f9248d0f3fa412e5376d9db78da914a8597c48e0e5f22470ce65f14242e53c6cac68fa3c86b3d049f33977a0d37f7bb863b33
6
+ metadata.gz: 5eccd278929da2180b8e7da31eb8efe53cf6711eef128dcd7ac31ff167c9f9691798ea1dc3d4cc8cd258c1a8e0e311e3afe27142126a70dbbfbdcc48c50edccf
7
+ data.tar.gz: a3910ea35830bdde568de7d1f05ef67d40e6ab26c5607f6013f76fab5f860cb824dd7e07e7032491e1bd80deba9910979b1367211f6addfdf12bc8b52bb8d0bd
data/README.md CHANGED
@@ -51,7 +51,19 @@ safe.nfs.get_directory("/mydir")["files"].each do |file|
51
51
  end
52
52
  ```
53
53
 
54
- ## Structured Data (SD):
54
+ ## Structured Data (SD) - With helpers:
55
+
56
+ ```ruby
57
+ safe = SafeNet::quick_start
58
+
59
+ safe.sd.create('my_sd', 'Hello SD!')
60
+ puts safe.sd.read('my_sd') # Hello SD!
61
+
62
+ safe.sd.update('my_sd', 'Hello SD 2!')
63
+ puts safe.sd.read('my_sd') # Hello SD 2!
64
+ ```
65
+
66
+ ## Structured Data (SD) - With safe primitives:
55
67
 
56
68
  ```ruby
57
69
  # client
@@ -61,8 +73,8 @@ safe = SafeNet::Client.new(permissions: ["LOW_LEVEL_API"])
61
73
  hnd_cipher = safe.cipher.get_handle
62
74
 
63
75
  # create
64
- name = SafeNet::s2h("my_sd")
65
- hnd = safe.sd.create(name, 500, hnd_cipher, IO.binread("#{Rails.root}/my_file.txt"))
76
+ name = SafeNet::s2b('my_sd')
77
+ hnd = safe.sd.create_sd(name, 500, hnd_cipher, IO.binread("#{Rails.root}/my_file.txt"))
66
78
  safe.sd.put(hnd) # saves on the network
67
79
  safe.sd.drop_handle(hnd) # release handler
68
80
 
@@ -70,16 +82,29 @@ safe.sd.drop_handle(hnd) # release handler
70
82
  safe.cipher.drop_handle(hnd_cipher)
71
83
 
72
84
  # read
73
- name = SafeNet::s2h("my_sd")
85
+ name = SafeNet::s2b('my_sd')
74
86
  hnd_sd_data_id = safe.data_id.get_data_id_sd(name)
75
87
  hnd_sd = safe.sd.get_handle(hnd_sd_data_id)['handleId']
76
- contents = safe.sd.read(hnd_sd)
88
+ contents = safe.sd.read_data(hnd_sd)
77
89
  safe.sd.drop_handle(hnd_sd)
78
90
  safe.data_id.drop_handle(hnd_sd_data_id)
79
91
  puts contents # print SD contents on screen
80
92
  ```
81
93
 
82
- ## Immutable Data:
94
+ ## Immutable Data - With helpers:
95
+ ```ruby
96
+ # client
97
+ safe = SafeNet::quick_start
98
+
99
+ # write
100
+ name = safe.immutable.write('Hello SD')
101
+ puts "Immutable name:\n * Binary: #{name}\n * Hex...: #{name.unpack("H*").first}\n * Base64: #{Base64.encode64(name)}"
102
+
103
+ # read
104
+ puts safe.immutable.read(name)
105
+ ```
106
+
107
+ ## Immutable Data - With safe primitives:
83
108
 
84
109
  ```ruby
85
110
  # client
@@ -95,12 +120,15 @@ hnd_data_id = safe.immutable.close_writer(hnd_w, hnd_cipher)
95
120
  name = safe.data_id.serialize(hnd_data_id) # IMMUTABLE NAME
96
121
  safe.immutable.drop_writer_handle(hnd_w)
97
122
  safe.data_id.drop_handle(hnd_data_id)
98
- puts "Immutable name: #{name}"
123
+ puts "Immutable name:\n * Binary: #{name}\n * Hex...: #{name.unpack("H*").first}\n * Base64: #{Base64.encode64(name)}"
124
+
125
+ # release cipher handler
126
+ safe.cipher.drop_handle(hnd_cipher)
99
127
 
100
128
  # read
101
129
  hnd_data_id = safe.data_id.deserialize(name)
102
130
  hnd_r = safe.immutable.get_reader_handle(hnd_data_id)
103
- contents = safe.immutable.read(hnd_r)
131
+ contents = safe.immutable.read_data(hnd_r)
104
132
  safe.immutable.drop_reader_handle(hnd_r)
105
133
  safe.data_id.drop_handle(hnd_data_id)
106
134
  puts contents
@@ -111,13 +139,13 @@ max_chunk_size = 100_000
111
139
 
112
140
  hnd_data_id = safe.data_id.deserialize(name)
113
141
  hnd_r = safe.immutable.get_reader_handle(hnd_data_id)
114
- contents = safe.immutable.read(hnd_r, "bytes=#{chunk_pos}-#{chunk_pos+max_chunk_size}")
142
+ contents = safe.immutable.read_data(hnd_r, "bytes=#{chunk_pos}-#{chunk_pos+max_chunk_size}")
115
143
  safe.immutable.drop_reader_handle(hnd_r)
116
144
  safe.data_id.drop_handle(hnd_data_id)
117
145
  puts contents
118
146
  ```
119
147
 
120
- <!-- id = SafeNet.s2h("my_sd")
148
+ <!-- id = SafeNet.s2b('my_sd')
121
149
  safe.sd.update(id, 500, "Hi John")
122
150
  safe.sd.get(id, 500)
123
151
  safe.sd.update(id, 500, "Hello World!") -->
data/lib/safenet.rb CHANGED
@@ -838,7 +838,7 @@ module SafeNet
838
838
  @client = client_obj
839
839
  end
840
840
 
841
- def create(name, type_tag = 500, hnd_cipher_opts = nil, data = nil, version = 0)
841
+ def create_sd(name, type_tag = 500, hnd_cipher_opts = nil, data = nil, version = 0)
842
842
  # Entry point
843
843
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data"
844
844
 
@@ -934,7 +934,7 @@ module SafeNet
934
934
  JSON.parse(res.body)
935
935
  end
936
936
 
937
- def read(handle_id, version = nil)
937
+ def read_data(handle_id, version = nil)
938
938
  # entry point
939
939
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/#{handle_id}"
940
940
  url = "#{url}/#{version}?" if ! version.nil?
@@ -960,6 +960,52 @@ module SafeNet
960
960
  res = http.request(req)
961
961
  res.code == "200" ? true : JSON.parse(res.body)
962
962
  end
963
+
964
+ def create(name, contents, type = 500)
965
+ name = name.is_a?(String) ? SafeNet::s2b(name) : name
966
+
967
+ # plain (not encrypted)
968
+ hnd_cipher = @client.cipher.get_handle
969
+
970
+ # create
971
+ hnd = @client.sd.create_sd(name, type, hnd_cipher, contents)
972
+ res = @client.sd.put(hnd) # saves on the network
973
+ @client.sd.drop_handle(hnd) # release handler
974
+
975
+ # release cipher handler
976
+ @client.cipher.drop_handle(hnd_cipher)
977
+
978
+ res
979
+ end
980
+
981
+ def read(name, type = 500)
982
+ name = name.is_a?(String) ? SafeNet::s2b(name) : name
983
+
984
+ hnd_sd_data_id = @client.data_id.get_data_id_sd(name)
985
+ hnd_sd = @client.sd.get_handle(hnd_sd_data_id)['handleId']
986
+ contents = @client.sd.read_data(hnd_sd)
987
+ @client.sd.drop_handle(hnd_sd)
988
+ @client.data_id.drop_handle(hnd_sd_data_id)
989
+
990
+ contents
991
+ end
992
+
993
+ def update(name, contents, type = 500)
994
+ name = name.is_a?(String) ? SafeNet::s2b(name) : name
995
+
996
+ # plain (not encrypted)
997
+ hnd_cipher = @client.cipher.get_handle
998
+
999
+ # create
1000
+ hnd = @client.sd.create_sd(name, type, hnd_cipher, contents)
1001
+ res = @client.sd.post(hnd) # saves on the network
1002
+ @client.sd.drop_handle(hnd) # release handler
1003
+
1004
+ # release cipher handler
1005
+ @client.cipher.drop_handle(hnd_cipher)
1006
+
1007
+ res
1008
+ end
963
1009
  end
964
1010
 
965
1011
  class Immutable
@@ -996,7 +1042,7 @@ module SafeNet
996
1042
  end
997
1043
 
998
1044
  # eg range: "bytes=0-1000"
999
- def read(handle_id, range = nil)
1045
+ def read_data(handle_id, range = nil)
1000
1046
  # entry point
1001
1047
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/#{handle_id}"
1002
1048
 
@@ -1070,6 +1116,40 @@ module SafeNet
1070
1116
  res = http.request(req)
1071
1117
  res.code == "200" ? true : JSON.parse(res.body)
1072
1118
  end
1119
+
1120
+ # helper
1121
+ def write(contents)
1122
+ # plain (not encrypted)
1123
+ hnd_cipher = @client.cipher.get_handle
1124
+
1125
+ # write
1126
+ hnd_w = @client.immutable.get_writer_handle
1127
+ @client.immutable.write_data(hnd_w, contents)
1128
+ hnd_data_id = @client.immutable.close_writer(hnd_w, hnd_cipher)
1129
+ name = @client.data_id.serialize(hnd_data_id)
1130
+ @client.immutable.drop_writer_handle(hnd_w)
1131
+ @client.data_id.drop_handle(hnd_data_id)
1132
+
1133
+ # release cipher handler
1134
+ @client.cipher.drop_handle(hnd_cipher)
1135
+
1136
+ name
1137
+ end
1138
+
1139
+ # helper
1140
+ def read(name, chunk_pos = nil, max_chunk_size = 1_000_000)
1141
+ hnd_data_id = @client.data_id.deserialize(name)
1142
+ hnd_r = @client.immutable.get_reader_handle(hnd_data_id)
1143
+ contents = if chunk_pos
1144
+ @client.immutable.read_data(hnd_r, "bytes=#{chunk_pos}-#{chunk_pos+max_chunk_size}")
1145
+ else
1146
+ @client.immutable.read_data(hnd_r)
1147
+ end
1148
+ @client.immutable.drop_reader_handle(hnd_r)
1149
+ @client.data_id.drop_handle(hnd_data_id)
1150
+
1151
+ contents
1152
+ end
1073
1153
  end
1074
1154
 
1075
1155
  class Cipher
@@ -1204,8 +1284,29 @@ module SafeNet
1204
1284
 
1205
1285
  end
1206
1286
 
1207
- def self.s2h(str)
1287
+ def self.quick_start(low_level_api = true, safe_drive_access = true)
1288
+ permissions = []
1289
+ permissions << 'LOW_LEVEL_API' if low_level_api
1290
+ permissions << 'SAFE_DRIVE_ACCESS' if safe_drive_access
1291
+ SafeNet::Client.new(permissions: permissions)
1292
+ end
1293
+
1294
+ def self.s2b(str)
1208
1295
  # Digest::SHA2.new(256).hexdigest(str)
1209
1296
  Digest::SHA2.new(256).base64digest(str)
1210
1297
  end
1298
+
1299
+ def self.bin_name(str)
1300
+ BinName.new(str)
1301
+ end
1302
+
1303
+ class BinName
1304
+ def initializer(str)
1305
+ @value = str
1306
+ end
1307
+
1308
+ def to_s
1309
+ @value
1310
+ end
1311
+ end
1211
1312
  end
@@ -1,3 +1,3 @@
1
1
  module Safenet
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-safenet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Loureiro