lib/fernet/secret.rb in fernet-1.6 vs lib/fernet/secret.rb in fernet-2.0.rc1
- old
+ new
@@ -1,20 +1,26 @@
+require 'base64'
module Fernet
class Secret
- def initialize(secret, encrypt)
- @secret = secret
- @encrypt = encrypt
+ class InvalidSecret < RuntimeError; end
+
+ def initialize(secret)
+ @secret = Base64.urlsafe_decode64(secret)
+ unless @secret.bytesize == 32
+ raise InvalidSecret, "Secret must be 32 bytes, instead got #{@secret.bytesize}"
+ end
end
def encryption_key
- @secret.slice(@secret.size/2, @secret.size)
+ @secret.slice(16, 16)
end
def signing_key
- if @encrypt
- @secret.slice(0, @secret.size/2)
- else
- @secret
- end
+ @secret.slice(0, 16)
end
+
+ def to_s
+ "<Fernet::Secret [masked]>"
+ end
+ alias to_s inspect
end
end