Class: Binenc::Java::SBLFactory
- Inherits:
-
Object
- Object
- Binenc::Java::SBLFactory
show all
- Includes:
- DataConversion, SBLDSL
- Defined in:
- lib/binenc/factory/sbl_factory/sbl_factory.rb
Overview
simple binary layout factory
Instance Method Summary
collapse
#from_b64, #from_hex, included, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str
Methods included from SBLDSL
#bin, #date, #int, #oid, #seq, #str
Instance Method Details
#define(&block) ⇒ Object
12
13
14
15
|
# File 'lib/binenc/factory/sbl_factory/sbl_factory.rb', line 12
def define(&block)
instance_eval(&block)
self
end
|
#encoded ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/binenc/factory/sbl_factory/sbl_factory.rb', line 17
def encoded
res = []
structure.each do |st|
res << self.send("#{to_instance_method_name(st)}").encoded(false)
end
ASN1Sequence.new(res).encoded
end
|
#from_bin(bin) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/binenc/factory/sbl_factory/sbl_factory.rb', line 26
def from_bin(bin)
seq = ASN1Object.decode(bin).value
if seq.length > structure.length
logger.warn "Given binary has more field (#{seq.length}) than the defined specification (#{structure.length}). Different version of structure?"
elsif structure.length > seq.length
logger.warn "Defined specification has more field (#{structure.length}) than given binary (#{seq.length}). Different version of structure?"
end
structure.each_index do |i|
name = structure[i]
val = seq[i]
create_instance_method(name, val)
end
self
end
|
#value_from_bin_struct(bin, *fieldNo) ⇒ Object
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
|
# File 'lib/binenc/factory/sbl_factory/sbl_factory.rb', line 46
def value_from_bin_struct(bin, *fieldNo)
seq = ASN1Object.decode(bin).value
ret = []
fieldNo.each do |fn|
raise BinencEngineException, "Given field no '#{fn}' to extract is larger than found fields (#{seq.length})" if fn > seq.length
v = seq[fn]
if v.is_a?(::Java::OrgBouncycastleAsn1::ASN1Object)
case v
when ::Java::OrgBouncycastleAsn1::DERBitString
vv = to_java_bytes(v.bytes)
when ::Java::OrgBouncycastleAsn1::DERUTF8String
vv = v.to_s
when ::Java::OrgBouncycastleAsn1::ASN1Integer
vv = v.value
when ::Java::OrgBouncycastleAsn1::ASN1GeneralizedTime
d = v.date
vv = Time.at(d.time/1000)
when ::Java::OrgBouncycastleAsn1::ASN1ObjectIdentifier
vv = v.id
else
raise BinencEngineException, "Unhandled ASN1 object '#{obj.class}'"
end
else
vv = v
end
ret << vv
end
ret
end
|