blob: adf3f7fb5a65c01f3f626eeff3c98aebcc64df6a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# frozen_string_literal: true
module Gitlab
module Database
# 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.
class Sha256Attribute < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Bytea
# Casts binary data to a SHA256 and remove trailing = and newline from encode64
def deserialize(value)
value = super(value)
if value.present?
Base64.encode64(value).delete("=").chomp("\n")
else
nil
end
end
# Casts a SHA256 in a proper binary format. which is 32 bytes long
def serialize(value)
arg = if value.present?
Base64.decode64(value)
else
nil
end
super(arg)
end
end
end
end
|