add class to wrap sensitive values
This commit is contained in:
39
lib/sensitive.rb
Normal file
39
lib/sensitive.rb
Normal file
@ -0,0 +1,39 @@
|
||||
class Sensitive
|
||||
alias_method :eql?, :==
|
||||
alias_method :equal?, :==
|
||||
|
||||
def initialize(v, ch: "*", head: 2, tail: 2)
|
||||
@v = v
|
||||
@ch = ch
|
||||
@head = head
|
||||
@tail = tail
|
||||
end
|
||||
|
||||
def mask(v)
|
||||
"".concat(v[0, @head], @ch * (v.length - (@head + @tail)), v[-@tail, @tail])
|
||||
end
|
||||
|
||||
def unwrap
|
||||
@v
|
||||
end
|
||||
|
||||
def length
|
||||
@v.length
|
||||
end
|
||||
|
||||
def to_s
|
||||
mask @v
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#<#{self.class.name} @v=#{wrap}>"
|
||||
end
|
||||
|
||||
def hash
|
||||
@v.hash
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
other.is_a?(Sensitive) && other.hash == hash
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user