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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/json/ld/signature/rsaSigner.rb', line 18
def sign(input, options = {} )
if options['creator'].nil?
raise JsonLdSignatureError::MissingCreator, "the creator of the signature must be identified"
end
creator = options['creator']
privateKey = @priv
unless privateKey.private?
raise JsonLdSignatureError::WrongKeyType, "submitted key is a public key"
end
jsonld = case input
when String then
begin
JSON.parse(input)
rescue JSON::ParserError => e
raise JsonLdSignatureError::InvalidJsonLdDocument, e.message
end
when Hash then input
else
raise JsonLdSignatureError::InvalidJsonLdDocument
end
jsonld.delete 'signature'
created = "2018-03-15T00:00:00Z"
nonce = options['nonce']
domain = options['domain']
normOpts = {
'nonce' => nonce,
'domain' => options['domain'],
'created' => created,
'creator' => creator
}
normalizedGraph = JSON::LD::SIGNATURE::generateNormalizedGraph jsonld, normOpts
digest = OpenSSL::Digest::SHA256.new
signature = privateKey.sign digest, normalizedGraph
enc = Base64.strict_encode64(signature)
sigobj = JSON.parse %({
"type" : "RsaSignature2017",
"creator" : "#{creator}",
"created" : "#{created}",
"signatureValue" : "#{enc}"
})
sigobj['domain'] = domain unless options['domain'].nil?
sigobj['nonce'] = nonce unless nonce.nil?
jsonld['signature'] = sigobj
jsonld.to_json
end
|