Class: VChainClient::Crypto

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

Constant Summary collapse

@@ecc_private_key =
nil
@@ec_ecc_private =
nil
@@rsa_private_key =
nil
@@ec_rsa_private =
nil
@@vchain_rsa_public_key =
nil

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Crypto



16
17
18
19
20
# File 'lib/vchain_client/crypto.rb', line 16

def initialize(config)
  @config = config

  @log = Log4r::Logger["vchain_client"]
end

Instance Method Details

#checkTreeSignature(tree_root_hash, blockchain_txid, blockchain_block_hash, blockchain_timestamp, blockstack_client_id, sig_version, signature, pubkey) ⇒ Object



747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/vchain_client/crypto.rb', line 747

def checkTreeSignature(tree_root_hash, blockchain_txid, blockchain_block_hash, blockchain_timestamp, blockstack_client_id, sig_version, signature, pubkey)

 what_to_check = tree_root_hash
 what_to_check += blockchain_txid
 what_to_check += blockchain_block_hash
 what_to_check += blockchain_timestamp.to_s
 what_to_check += blockstack_client_id
 what_to_check += sig_version

      if @log.debug?
        @log.debug("[Crypto.checkTreeSignature] input:")
    @log.debug("-> tree_root_hash: #{tree_root_hash}")
    @log.debug("-> blockchain_txid: #{blockchain_txid}")
    @log.debug("-> blockchain_block_hash: #{blockchain_block_hash}")
    @log.debug("-> blockchain_timestamp: #{blockchain_timestamp}")
    @log.debug("-> blockstack_client_id: #{blockstack_client_id}")
    @log.debug("-> sig_version: #{sig_version}")
    @log.debug("-> signature: "+ Base64.encode64(signature))
    @log.debug("-> pubkey: #{pubkey}")
      end

 begin 
  
  return self.verifySignature(what_to_check, signature, pubkey)

 rescue => e
  if @log.error?
    @log.error("[Crypto.checkTreeSignature] verifySignature raised exception:")
    @log.error("#{e.class}, #{e.message}")
    @log.error("-> tree_root_hash: #{tree_root_hash}")
    @log.error("-> blockchain_txid: #{blockchain_txid}")
    @log.error("-> blockchain_block_hash: #{blockchain_block_hash}")
    @log.error("-> blockchain_timestamp: #{blockchain_timestamp}")
    @log.error("-> blockstack_client_id: #{blockstack_client_id}")
    @log.error("-> sig_version: #{sig_version}")
    @log.error("-> signature: "+ Base64.encode64(signature))
    @log.error("-> pubkey: #{pubkey}")
    @log.error("--> what_to_check: #{what_to_check}")
    @log.error("--> signature: "+ Base64.encode64(signature))
    @log.error("--> pubkey: #{pubkey}")
  end

  raise e
 end
end

#checkVerificationSignature(field_hash, data_hash, doc_hash, credentials_hash, verification_type, weight, timestamp, blockstack_client_id, pubkey, signature, version) ⇒ Object



793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/vchain_client/crypto.rb', line 793

def checkVerificationSignature(field_hash, data_hash, doc_hash, credentials_hash, verification_type, weight, timestamp, blockstack_client_id, pubkey, signature, version)

      if @log.debug?
        @log.debug("[Crypto.checkVerificationSignature] input:")
    @log.debug("-> field_hash: #{field_hash}")
    @log.debug("-> data_hash: #{data_hash}")
    @log.debug("-> doc_hash: #{doc_hash}")
    @log.debug("-> credentials_hash: #{credentials_hash}")
    @log.debug("-> type: #{verification_type}")
    @log.debug("-> weight: "+ weight.to_s)
    @log.debug("-> timestamp: "+ timestamp.to_s)
    @log.debug("-> blockstack_client_id: #{blockstack_client_id}")
    @log.debug("-> signature: "+ Base64.encode64(signature))
    @log.debug("-> pubkey: #{pubkey}")
      end

      what_to_check = field_hash
      what_to_check += data_hash
      what_to_check += doc_hash
      what_to_check += credentials_hash
      what_to_check += verification_type
      what_to_check += weight.to_s
      what_to_check += timestamp.to_s
      what_to_check += blockstack_client_id
      what_to_check += version

      begin
      
        return self.verifySignature(what_to_check, signature, pubkey)

      rescue => e
  if @log.error?
    @log.error("[Crypto.checkVerificationSignature] verifySignature raised exception:")
    @log.error("#{e.class}, #{e.message}")
    @log.error("-> field_hash: #{field_hash}")
    @log.error("-> data_hash: #{data_hash}")
    @log.error("-> doc_hash: #{doc_hash}")
    @log.error("-> credentials_hash: #{credentials_hash}")
    @log.error("-> verification_type: #{verification_type}")
    @log.error("-> timestamp: "+ timestamp.to_s)
    @log.error("-> weight: "+ weight.to_s)
    @log.error("-> blockstack_client_id: #{blockstack_client_id}")
    @log.error("-> signature: "+ Base64.encode64(signature))
    @log.error("-> pubkey: #{pubkey}")
  end

  raise e
      end
end

#decodeCypher(encoded_payload, key, iv) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vchain_client/crypto.rb', line 152

def decodeCypher(encoded_payload, key, iv)

      cifd = OpenSSL::Cipher.new('AES-256-CTR')

      cifd.decrypt

      cifd.key = key
      cifd.iv  = iv

      decoded = ''
      decoded << cifd.update(encoded_payload)
      decoded << cifd.final

      return decoded
end

#decodeRSA(encoded_data) ⇒ Object



68
69
70
71
72
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/vchain_client/crypto.rb', line 68

def decodeRSA(encoded_data)

      priv_key_path = @config["rsa_private_key_location"]

      if @log.debug?
        @log.debug("[Crypto.decodeRSA] input:")
        @log.debug("-> key path: #{priv_key_path}")
        @log.debug("-> input:")
        @log.debug(encoded_data)
      end

      if @@rsa_private_key == nil

        begin

@@rsa_private_key = File.read(priv_key_path)

        rescue => e
if @log.error?
  @log.error("[Crypto.decodeRSA] File.read raised exception:")
  @log.error("#{e.class}, #{e.message}")
  @log.error("--> priv_key_path: #{priv_key_path}")
  @log.error("-> input:")
  @log.error(encoded_data)
end

raise e
        end

        if @log.debug?
@log.debug("[Crypto.decodeRSA] priv key is loaded")
        end

      end

      if @@rsa_private_key == nil
        if @log.error?
@log.error("[Crypto.decodeRSA] failed to load private key")
@log.error("--> priv_key_path: #{priv_key_path}")
@log.error("-> input:")
@log.error(encoded_data)
        end

        return nil
      end

      if @@ec_rsa_private == nil

        begin

@@ec_rsa_private = OpenSSL::PKey::RSA.new(@@rsa_private_key)

        rescue => e
if @log.error?
  @log.error("[Crypto.decodeRSA] OpenSSL::PKey::EC.new raised exception:")
  @log.error("#{e.class}, #{e.message}")
  @log.error("--> priv_key_path: #{priv_key_path}")
  @log.error("-> input:")
  @log.error(encoded_data)
end

raise e
        end

        if @log.debug?
@log.debug("[Crypto.decodeRSA] key initialized")
        end

      end

      if @@ec_rsa_private == nil
        if @log.error?
@log.error("[Crypto.decodeRSA] failed init EC key")
@log.error("--> priv_key_path: #{priv_key_path}")
@log.error("-> input:")
@log.error(encoded_data)
        end

        return nil
      end

    return @@ec_rsa_private.private_decrypt(encoded_data, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
end

#encodeCypher(document) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/vchain_client/crypto.rb', line 168

def encodeCypher(document)
      cif = OpenSSL::Cipher.new('AES-256-CTR')

      cif.encrypt

      cif.key = key = cif.random_key
      cif.iv  = iv  = cif.random_iv

      out = {
        "payload" => (cif.update(document) + cif.final),
        "key" => key,
        "iv" => iv
      }

      return out
end

#encodeRSA(payload) ⇒ Object



62
63
64
65
66
# File 'lib/vchain_client/crypto.rb', line 62

def encodeRSA(payload)
  vchain_public_key = self.getVChainPublicKeyRSA()

  return vchain_public_key.public_encrypt(payload, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
end

#getVChainPublicKeyRSAObject



22
23
24
25
26
27
28
29
30
31
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
# File 'lib/vchain_client/crypto.rb', line 22

def getVChainPublicKeyRSA()

      if @@vchain_rsa_public_key != nil
        return @@vchain_rsa_public_key
      end

      blockstackClient = VChainClient::BlockstackClient.new(@config)

      vchain_public_key_body = nil

      begin

        vchain_public_key_body = blockstackClient.getPublicKeyRSA("vchain_core_01.id")

      rescue => e
        if @log.error?
 @log.error("[check] failed to retrieve vchain public RSA key from Blockstack")
 @log.error("#{e.class}, #{e.message}")
        end

        raise e
      end

      if vchain_public_key_body == nil
        if @log.error?
 @log.error("[check] failed to retrieve vchain public RSA key from Blockstack")
        end

        return false
      end

      vchain_public_key_str = "-----BEGIN PUBLIC KEY-----\n"
      vchain_public_key_str += vchain_public_key_body
      vchain_public_key_str += "\n-----END PUBLIC KEY-----"

      @@vchain_rsa_public_key = OpenSSL::PKey::RSA.new(vchain_public_key_str)

      return @@vchain_rsa_public_key
end

#signBatchRequest(batch, timestamp) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/vchain_client/crypto.rb', line 185

def signBatchRequest(batch, timestamp)
      OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)

      priv_key_path = @config["ecc_private_key_location"]

      if @log.debug?
        @log.debug("[Crypto.signBatchRequest] input:")
        @log.debug("-> timestamp: "+ timestamp.to_s)
        @log.debug("-> key path: #{priv_key_path}")
        @log.debug("-> input:")
        @log.debug(batch)
      end

      if @@ecc_private_key == nil

        begin

@@ecc_private_key = File.read(priv_key_path)

        rescue => e
if @log.error?
  @log.error("[Crypto.signBatchRequest] File.read raised exception:")
  @log.error("#{e.class}, #{e.message}")
  @log.error("-> timestamp: "+ timestamp.to_s)
  @log.error("--> priv_key_path: #{priv_key_path}")
  @log.error("-> input:")
  @log.error(batch)
end

raise e
        end

        if @log.debug?
@log.debug("[Crypto.signBatchRequest] priv key is loaded")
        end

      end

      if @@ecc_private_key == nil
        if @log.error?
@log.error("[Crypto.signBatchRequest] failed to load private key")
@log.error("-> timestamp: "+ timestamp.to_s)
@log.error("--> priv_key_path: #{priv_key_path}")
@log.error("-> input:")
@log.error(batch)
        end

        return nil
      end

      if @@ec_ecc_private == nil

        begin

@@ec_ecc_private = OpenSSL::PKey::EC.new(@@ecc_private_key)

        rescue => e
if @log.error?
  @log.error("[Crypto.signBatchRequest] OpenSSL::PKey::EC.new raised exception:")
  @log.error("#{e.class}, #{e.message}")
  @log.error("-> timestamp: "+ timestamp.to_s)
  @log.error("--> priv_key_path: #{priv_key_path}")
  @log.error("-> input:")
  @log.error(batch)
end

raise e
        end

        if @log.debug?
@log.debug("[Crypto.signBatchRequest] key initialized")
        end

      end

      if @@ec_ecc_private == nil
        if @log.error?
@log.error("[Crypto.signBatchRequest] failed init EC key")
@log.error("-> timestamp: "+ timestamp.to_s)
@log.error("--> priv_key_path: #{priv_key_path}")
@log.error("-> input:")
@log.error(batch)
        end

        return nil
      end

      digest = OpenSSL::Digest::SHA256.new

      whole_sign = ""

      batch.each { |batch_element|
        whole_sign += batch_element["data"].to_json 
        whole_sign += batch_element["point_type"]
        whole_sign += batch_element["weight"].to_s
        whole_sign += timestamp.to_s
      }

      if @log.debug?
        @log.debug("[Crypto.signBatchRequest] whole_to_sign: "+ whole_sign)
      end

      whole_signature = nil

      begin
        
        whole_signature = @@ec_ecc_private.sign(digest, whole_sign)

      rescue => e
        if @log.error?
@log.error("[Crypto.signBatchRequest] ec.sign raised exception:")
@log.error("#{e.class}, #{e.message}")
@log.error("-> timestamp: "+ timestamp.to_s)
@log.error("--> priv_key_path: #{priv_key_path}")
@log.error("--> whole_sign: #{whole_sign}")
@log.error("-> input:")
@log.error(batch)
        end

        raise e
      end

      if whole_signature == nil
        if @log.error?
@log.error("[Crypto.signBatchRequest] failed to sign")
@log.error("-> timestamp: "+ timestamp.to_s)
@log.error("--> priv_key_path: #{priv_key_path}")
@log.error("--> whole_sign: #{whole_sign}")
@log.error("-> input:")
@log.error(batch)
        end

        return nil
      end

      if @log.debug?
        @log.debug("[Crypto.signBatchRequest] whole_signature raw: "+ Base64.encode64(whole_signature))
      end

      return Base64.encode64(whole_signature).gsub(/\n/, "")
end

#signDataPoint(point_type, data, doc_hash, credentials_hash, weight, timestamp) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/vchain_client/crypto.rb', line 536

def signDataPoint(point_type, data, doc_hash, credentials_hash, weight, timestamp)

  OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)

  this_client_id = @config["blockstack"]["client_id"]
  priv_key_path = @config["ecc_private_key_location"]

      if @log.debug?
@log.debug("[Crypto.signDataPoint] input:")
@log.debug("-> this_client_id: #{this_client_id}")
@log.debug("-> doc_hash: #{doc_hash}")
@log.debug("-> credentials_hash: #{credentials_hash}")
@log.debug("-> weight: "+ weight.to_s)
@log.debug("-> timestamp: "+ timestamp.to_s)
@log.debug("-> point_type: #{point_type}")
@log.debug("-> key path: #{priv_key_path}")
@log.debug("-> data:")
@log.debug(data)
      end

      if @@ecc_private_key == nil

begin

  @@ecc_private_key = File.read(priv_key_path)

rescue => e
  if @log.error?
    @log.error("[Crypto.signDataPoint] File.read raised exception:")
    @log.error("#{e.class}, #{e.message}")
    @log.error("-> this_client_id: #{this_client_id}")
    @log.error("-> doc_hash: #{doc_hash}")
    @log.error("-> credentials_hash: #{credentials_hash}")
    @log.error("-> weight: "+ weight.to_s)
    @log.error("-> timestamp: "+ timestamp.to_s)
    @log.error("-> point_type: #{point_type}")
    @log.error("-> key path: #{priv_key_path}")
    @log.error("-> data:")
    @log.error(data)
  end

  raise e
end

if @log.debug?
  @log.debug("[Crypto.signDataPoint] priv key loaded")
end

      end

      if @@ecc_private_key == nil
if @log.error?
  @log.error("[Crypto.signDataPoint] failed to load private key")
  @log.error("-> this_client_id: #{this_client_id}")
  @log.error("-> doc_hash: #{doc_hash}")
  @log.error("-> credentials_hash: #{credentials_hash}")
  @log.error("-> weight: "+ weight.to_s)
  @log.error("-> timestamp: "+ timestamp.to_s)
  @log.error("-> point_type: #{point_type}")
  @log.error("-> key path: #{priv_key_path}")
  @log.error("-> data:")
  @log.error(data)
end

return nil
      end

      if @@ec_ecc_private == nil

begin
  
  @@ec_ecc_private = OpenSSL::PKey::EC.new(@@ecc_private_key)

rescue => e
  if @log.error?
    @log.error("[Crypto.signDataPoint] OpenSSL::PKey::EC.new raised exception:")
    @log.error("#{e.class}, #{e.message}")
    @log.error("-> this_client_id: #{this_client_id}")
    @log.error("-> doc_hash: #{doc_hash}")
    @log.error("-> credentials_hash: #{credentials_hash}")
    @log.error("-> weight: "+ weight.to_s)
    @log.error("-> timestamp: "+ timestamp.to_s)
    @log.error("-> point_type: #{point_type}")
    @log.error("-> key path: #{priv_key_path}")
    @log.error("-> data:")
    @log.error(data)
    @log.error("--> priv_key_path: #{priv_key_path}")
  end

  raise e
end

if @log.debug?
  @log.debug("[Crypto.signDataPoint] key created")
end

      end

      if @@ec_ecc_private == nil
if @log.error?
  @log.error("[Crypto.signDataPoint] failed init EC key")
  @log.error("-> this_client_id: #{this_client_id}")
  @log.error("-> doc_hash: #{doc_hash}")
  @log.error("-> credentials_hash: #{credentials_hash}")
  @log.error("-> weight: "+ weight.to_s)
  @log.error("-> timestamp: "+ timestamp.to_s)
  @log.error("-> point_type: #{point_type}")
  @log.error("-> key path: #{priv_key_path}")
  @log.error("-> data:")
  @log.error(data)
  @log.error("--> priv_key_path: #{priv_key_path}")
end

return nil
      end

      digest = OpenSSL::Digest::SHA256.new

  output = {}

  data.each{ |rec|
    field = rec[0]
    value = rec[1]

    if @log.debug?
      @log.debug("[Crypto.signDataPoint] field: #{field}, value: #{value}")
    end

    if field != 'client_id'

      field_hash = Digest::SHA512.hexdigest(field)

      value_hash = Digest::SHA512.hexdigest(value)

      what_to_sign = field_hash
      what_to_sign += value_hash
      what_to_sign += Digest::SHA512.hexdigest(doc_hash)
      what_to_sign += Digest::SHA512.hexdigest(credentials_hash)
      what_to_sign += point_type
      what_to_sign += weight.to_s
      what_to_sign += timestamp.to_s
      what_to_sign += this_client_id
      what_to_sign += VChainClient::Client::DATA_POINT_VERSION

      if @log.debug?
        @log.debug("[Crypto.signDataPoint] field_hash: #{field_hash}")
        @log.debug("[Crypto.signDataPoint] value_hash: #{value_hash}")
      end

  signature = nil

  begin
    
    signature = @@ec_ecc_private.sign(digest, what_to_sign)

  rescue => e
    if @log.error?
      @log.error("[Crypto.signDataPoint] ec.sign raised exception:")
      @log.error("#{e.class}, #{e.message}")
      @log.error("-> this_client_id: #{this_client_id}")
      @log.error("-> doc_hash: #{doc_hash}")
      @log.error("-> credentials_hash: #{credentials_hash}")
      @log.error("-> weight: "+ weight.to_s)
      @log.error("-> timestamp: "+ timestamp.to_s)
      @log.error("-> point_type: #{point_type}")
      @log.error("-> key path: #{priv_key_path}")
      @log.error("-> data:")
      @log.error(data)
      @log.error("--> priv_key_path: #{priv_key_path}")
      @log.error("--> what_to_sign: #{what_to_sign}")
    end

    raise e
  end

  if signature == nil
    if @log.error?
      @log.error("[Crypto.signDataPoint] failed to sign")
      @log.error("-> this_client_id: #{this_client_id}")
      @log.error("-> doc_hash: #{doc_hash}")
      @log.error("-> credentials_hash: #{credentials_hash}")
      @log.error("-> weight: "+ weight.to_s)
      @log.error("-> timestamp: "+ timestamp.to_s)
      @log.error("-> point_type: #{point_type}")
      @log.error("-> key path: #{priv_key_path}")
      @log.error("-> data:")
      @log.error(data)
      @log.error("--> priv_key_path: #{priv_key_path}")
      @log.error("--> what_to_sign: #{what_to_sign}")
    end

    return nil
  end

  if @log.debug?
    @log.debug("[Crypto.signDataPoint] signature raw: "+ Base64.encode64(signature))
  end

      output[field] = Base64.encode64(signature).gsub(/\n/, "")

    end
  }

      if @log.debug?
@log.debug("[Crypto.signDataPoint] output:")
@log.debug(output)
      end

  return output
end

#signRequest(document, point_type, weight, timestamp) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/vchain_client/crypto.rb', line 327

def signRequest(document, point_type, weight, timestamp)
  
  OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)

  priv_key_path = @config["ecc_private_key_location"]

  if @log.debug?
    @log.debug("[Crypto.signRequest] input:")
    @log.debug("-> point_type: "+ point_type)
    @log.debug("-> weight: "+ weight.to_s)
    @log.debug("-> timestamp: "+ timestamp.to_s)
    @log.debug("-> key path: #{priv_key_path}")
    @log.debug("-> input:")
    @log.debug(document)
  end

  if @@ecc_private_key == nil

    begin

      @@ecc_private_key = File.read(priv_key_path)

    rescue => e
      if @log.error?
        @log.error("[Crypto.signRequest] File.read raised exception:")
        @log.error("#{e.class}, #{e.message}")
        @log.error("-> point_type: "+ point_type)
        @log.error("-> weight: "+ weight.to_s)
        @log.error("-> timestamp: "+ timestamp.to_s)
        @log.error("-> input:")
        @log.error(document)
        @log.error("--> priv_key_path: #{priv_key_path}")
      end

      raise e
    end

    if @log.debug?
      @log.debug("[Crypto.signRequest] priv key is loaded")
    end

  end

  if @@ecc_private_key == nil
    if @log.error?
      @log.error("[Crypto.signRequest] failed to load private key")
      @log.error("-> point_type: "+ point_type)
      @log.error("-> weight: "+ weight.to_s)
      @log.error("-> timestamp: "+ timestamp.to_s)
      @log.error("-> input:")
      @log.error(document)
      @log.error("--> priv_key_path: #{priv_key_path}")
    end

    return nil
  end

  if @@ec_ecc_private == nil

    begin
      
      @@ec_ecc_private = OpenSSL::PKey::EC.new(@@ecc_private_key)

    rescue => e
      if @log.error?
        @log.error("[Crypto.signRequest] OpenSSL::PKey::EC.new raised exception:")
        @log.error("#{e.class}, #{e.message}")
        @log.error("-> point_type: "+ point_type)
        @log.error("-> weight: "+ weight.to_s)
        @log.error("-> timestamp: "+ timestamp.to_s)
        @log.error("-> input:")
        @log.error(document)
        @log.error("--> priv_key_path: #{priv_key_path}")
      end

      raise e
    end

    if @log.debug?
      @log.debug("[Crypto.signRequest] key initialized")
    end

  end

  if @@ec_ecc_private == nil
    if @log.error?
      @log.error("[Crypto.signRequest] failed init EC key")
      @log.error("-> point_type: "+ point_type)
      @log.error("-> weight: "+ weight.to_s)
      @log.error("-> timestamp: "+ timestamp.to_s)
      @log.error("-> input:")
      @log.error(document)
      @log.error("--> priv_key_path: #{priv_key_path}")
    end

    return nil
  end

  digest = OpenSSL::Digest::SHA256.new

  whole_sign = document.to_json + point_type + weight.to_s + timestamp.to_s

  if @log.debug?
    @log.debug("[Crypto.signRequest] whole_to_sign: "+ whole_sign)
  end

  whole_signature = nil

  begin
    
    whole_signature = @@ec_ecc_private.sign(digest, whole_sign)

  rescue => e
    if @log.error?
      @log.error("[Crypto.signRequest] ec.sign raised exception:")
      @log.error("#{e.class}, #{e.message}")
      @log.error("-> point_type: "+ point_type)
      @log.error("-> weight: "+ weight.to_s)
      @log.error("-> timestamp: "+ timestamp.to_s)
      @log.error("-> input:")
      @log.error(document)
      @log.error("--> priv_key_path: #{priv_key_path}")
      @log.error("--> whole_sign: #{whole_sign}")
    end

    raise e
  end

  if whole_signature == nil
    if @log.error?
      @log.error("[Crypto.signRequest] failed to sign")
      @log.error("-> point_type: "+ point_type)
      @log.error("-> weight: "+ weight.to_s)
      @log.error("-> timestamp: "+ timestamp.to_s)
      @log.error("-> input:")
      @log.error(document)
      @log.error("--> priv_key_path: #{priv_key_path}")
      @log.error("--> whole_sign: #{whole_sign}")
    end

    return nil
  end

  if @log.debug?
    @log.debug("[Crypto.signRequest] whole_signature raw: "+ Base64.encode64(whole_signature))
  end

  return Base64.encode64(whole_signature).gsub(/\n/, "")
end

#verifySignature(what_to_check, signature, public_key) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/vchain_client/crypto.rb', line 477

def verifySignature(what_to_check, signature, public_key)
  pub_key = "-----BEGIN PUBLIC KEY-----\n"
  pub_key += public_key
      pub_key += "\n-----END PUBLIC KEY-----"

      ec = nil

      begin

ec = OpenSSL::PKey::EC.new(pub_key)

      rescue => e
if @log.error?
  @log.error("[Crypto.verifySignature] OpenSSL::PKey::EC.new raised exception:")
  @log.error("#{e.class}, #{e.message}")
  @log.error("-> what_to_check: #{what_to_check}")
  @log.error("-> signature: "+ Base64.encode64(signature))
  @log.error("-> public_key: "+ pub_key)
  @log.error(document)
  @log.error("--> pub_key: #{pub_key}")
end

raise e
      end

      if ec == nil
if @log.error?
  @log.error("[Crypto.verifySignature] failed init EC key")
  @log.error("-> what_to_check: #{what_to_check}")
  @log.error("-> signature: "+ Base64.encode64(signature))
  @log.error("-> public_key: "+ pub_key)
  @log.error("--> pub_key: #{pub_key}")
end

return false
      end

      digest = OpenSSL::Digest::SHA256.new

      begin

return ec.verify(digest, signature, what_to_check)

      rescue => e
if @log.error?
  @log.error("[Crypto.verifySignature] ec.verify raised exception:")
  @log.error("#{e.class}, #{e.message}")
  @log.error("-> what_to_check: #{what_to_check}")
  @log.error("-> signature: "+ Base64.encode64(signature))
  @log.error("-> public_key: "+ pub_key)
  @log.error(document)
  @log.error("--> signature: "+ Base64.encode64(signature))
  @log.error("--> what_to_check: #{what_to_check}")
end

raise e
      end
end