Class: Ccrypto::Java::HMACEngine
- Inherits:
-
Object
- Object
- Ccrypto::Java::HMACEngine
show all
- Includes:
- DataConversion, TR::CondUtils
- Defined in:
- lib/ccrypto/java/engines/hmac_engine.rb
Defined Under Namespace
Classes: HMACEngineError, SupportedHMACList
Class Method Summary
collapse
Instance Method Summary
collapse
#from_b64, #from_b64_mime, #from_hex, included, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str
Constructor Details
#initialize(*args, &block) ⇒ HMACEngine
Returns a new instance of HMACEngine.
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
|
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 90
def initialize(*args, &block)
@config = args.first
logger.debug "HMAC Config : #{@config.inspect}"
raise HMACEngineException, "HMAC config is expected" if not @config.is_a?(Ccrypto::HMACConfig)
raise HMACEngineException, "Signing key is required" if is_empty?(@config.ccrypto_key)
raise HMACEngineException, "Ccrypto:SecretKey is required. Given #{@config.ccrypto_key.class}" if not @config.ccrypto_key.is_a?(Ccrypto::SecretKey)
begin
macAlgo = @config.provider_config[:hmac_algo]
prov = @config.provider_config[:jce_provider]
if not_empty?(prov)
logger.debug "Mac algo : #{macAlgo} with provider '#{prov}'"
@hmac = javax.crypto.Mac.getInstance(macAlgo, prov)
else
logger.debug "Mac algo : #{macAlgo} with null provider"
@hmac = javax.crypto.Mac.getInstance(macAlgo)
end
logger.debug "Initialize the Mac with ccrypto_key"
@hmac.init(@config.ccrypto_key.native_key)
rescue Exception => ex
raise HMACEngineException, ex
end
end
|
Class Method Details
.default_hmac_digest_algo ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 68
def self.default_hmac_digest_algo
primary = find_supported_hmac_by_digest_algo("sha3-256").first
if is_empty?(primary)
secondary = find_supported_hmac_by_digest_algo("sha256").first
if is_empty?(secondary)
first = supported_hmac.values.first
logger.debug "Both SHA3-256 and SHA256 are not supported. Default to '#{first.inspect}'"
first
else
secondary
end
else
primary
end
end
|
.find_supported_hmac_by_digest(digest) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 57
def self.find_supported_hmac_by_digest(digest)
case digest
when Symbol, String
supported_hmac.find( algo: digest )
when Ccrypto::DigestConfig
supported_hmac.select { |hm| hm.digest_config.algo.to_s.downcase == digest.algo.to_s.downcase }
else
raise HMACEngineException, "Unsupported parameter for digest. Expected Ccrypto::DigestConfig, symbol or string. Got '#{digest.class}'"
end
end
|
.supported_hmac ⇒ Object
Also known as:
supported_hmac_configs
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
|
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 19
def self.supported_hmac
if @supported.nil?
@supported = SupportedHMACList.new
Ccrypto::Java::DigestEngine.supported.each do |v|
begin
prov = v.provider_config[:jceProvider]
digestAlgo = v.provider_config[:algo_name]
if digestAlgo =~ /^SHA-/
digestAlgo = digestAlgo.gsub("-","")
end
algo = "HMAC#{digestAlgo}"
if not_empty?(prov)
logger.debug "Initializing HMAC algo '#{algo}' with provider '#{prov}'"
javax.crypto.Mac.getInstance(algo, prov)
else
logger.debug "Initializing HMAC algo '#{algo}' with null provider"
javax.crypto.Mac.getInstance(algo)
end
conf = Ccrypto::HMACConfig.new(v.dup)
conf.provider_config = { hmac_algo: algo, jce_provider: prov }
@supported.register(conf, { tag_under: :algo, tag_value: digestAlgo })
rescue Exception => ex
logger.debug "HMAC algo '#{algo}' failed. Error was : #{ex.message}"
end
end
end
@supported
end
|
Instance Method Details
#hmac_digest(val, output = :binary) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 128
def hmac_digest(val, output = :binary)
hmac_update(val)
res = hmac_final
case output
when :hex
to_hex(res)
when :b64
to_b64(res)
else
res
end
end
|
#hmac_final ⇒ Object
124
125
126
|
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 124
def hmac_final
@hmac.doFinal
end
|
#hmac_update(val) ⇒ Object
120
121
122
|
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 120
def hmac_update(val)
@hmac.update(to_java_bytes(val)) if not_empty?(val)
end
|