Class: Gitlab::Database::Sha256Attribute

Inherits:
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Bytea
  • Object
show all
Defined in:
lib/gitlab/database/sha256_attribute.rb

Overview

Class for casting binary data to hexadecimal SHA256 hashes (and vice-versa).

Using Sha256Attribute allows you to store SHA256 values as binary while still using them as if they were stored as string values. This gives you the ease of use of string values, but without the storage overhead.

Instance Method Summary collapse

Instance Method Details

#deserialize(value) ⇒ Object

Casts binary data to a SHA256 and remove trailing = and newline from encode64



12
13
14
15
16
17
18
19
# File 'lib/gitlab/database/sha256_attribute.rb', line 12

def deserialize(value)
  value = super(value)
  if value.present?
    Base64.encode64(value).delete("=").chomp("\n")
  else
    nil
  end
end

#serialize(value) ⇒ Object

Casts a SHA256 in a proper binary format. which is 32 bytes long



22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/database/sha256_attribute.rb', line 22

def serialize(value)
  arg = if value.present?
          Base64.decode64(value)
        else
          nil
        end

  super(arg)
end