Class: Ccrypto::SecretKey

Inherits:
Object
  • Object
show all
Includes:
Java::DataConversion
Defined in:
lib/ccrypto/java/ext/secret_key.rb

Instance Method Summary collapse

Methods included from Java::DataConversion

#from_b64, #from_b64_mime, #from_hex, included, #to_b64, #to_b64_mime, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(algo, keysize, key) ⇒ SecretKey

Returns a new instance of SecretKey.



8
9
10
11
12
13
# File 'lib/ccrypto/java/ext/secret_key.rb', line 8

def initialize(algo, keysize, key)
  @algo = algo
  @keysize = keysize
  @native_key = key
  @native_key = to_jce_secret_key
end

Instance Method Details

#==(val) ⇒ Object



77
78
79
# File 'lib/ccrypto/java/ext/secret_key.rb', line 77

def ==(val)
  self.equals?(val)
end

#equals?(key) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ccrypto/java/ext/secret_key.rb', line 59

def equals?(key)
  case key
  when Ccrypto::SecretKey
    logger.debug "Given key is Ccrypto::SecretKey"
    to_jce_secret_key.encoded == key.to_jce_secret_key.encoded
  when javax.crypto.spec.SecretKeySpec
    logger.debug "Given key is java SecretKeySpec"
    to_jce_secret_key.encoded == key.encoded
  when ::Java::byte[]
    to_jce_secret_key.encoded == key
  when String
    to_jce_secret_key.encoded == to_java_bytes(key)
  else
    logger.debug "Not sure how to compare : #{self} / #{key}"
    to_jce_secret_key == key
  end
end

#lengthObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/ccrypto/java/ext/secret_key.rb', line 48

def length
  case @native_key
  when javax.crypto.spec.SecretKeySpec
    @native_key.encoded.length
  when ::Java::byte[], String
    @native_key.length
  else
    @native_key.key.encoded.length
  end
end

#to_binObject



39
40
41
42
43
44
45
46
# File 'lib/ccrypto/java/ext/secret_key.rb', line 39

def to_bin
  case @native_key
  when javax.crypto.spec.SecretKeySpec
    @native_key.encoded
  else
    raise Ccrypto::Error, "Unsupported key type #{@native_key.class}"
  end
end

#to_jce_secret_keyObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ccrypto/java/ext/secret_key.rb', line 15

def to_jce_secret_key
  case @native_key
  when javax.crypto.spec.SecretKeySpec
    @native_key
  when ::Java::byte[]
    javax.crypto.spec.SecretKeySpec.new(@native_key, @algo.to_s)

  when String
    javax.crypto.spec.SecretKeySpec.new(to_java_bytes(@native_key), @algo.to_s)

  else
    case @native_key.ccrypto_key
    when javax.crypto.spec.SecretKeySpec
      @native_key.ccrypto_key
    when ::Java::byte[]
      javax.crypto.spec.SecretKeySpec.new(@native_key.ccrypto_key, @algo.to_s)
    when Ccrypto::SecretKey
      @native_key.ccrypto_key.native_key
    else
      raise Ccrypto::Error, "Unknown key to conver to jce #{@native_key.ccrypto_key}"
    end
  end
end