Class: Enquo::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/enquo/field.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*_) ⇒ Object

Raises:

  • (RuntimeError)


5
6
7
# File 'lib/enquo/field.rb', line 5

def self.new(*_)
	raise RuntimeError, "Enquo::Field cannot be instantiated directly; use Enquo::Crypto#field instead"
end

Instance Method Details

#decrypt_bool(data, ctx) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/enquo/field.rb', line 21

def decrypt_bool(data, ctx)
	unless data.is_a?(String)
		raise ArgumentError, "Enquo::Field#decrypt_i64 can only decrypt from a string (got #{data.class})"
	end

	unless data.encoding == Encoding::UTF_8 && data.valid_encoding?
		raise ArgumentError, "Enquo::Field#decrypt_i64 can only decrypt validly-encoded UTF-8 strings (got #{data.encoding})"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_decrypt_bool(data, ctx)
end

#decrypt_date(data, ctx) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/enquo/field.rb', line 85

def decrypt_date(data, ctx)
	unless data.is_a?(String)
		raise ArgumentError, "Enquo::Field#decrypt_date can only decrypt from a string (got #{data.class})"
	end

	unless data.encoding == Encoding::UTF_8 && data.valid_encoding?
		raise ArgumentError, "Enquo::Field#decrypt_date can only decrypt validly-encoded UTF-8 strings (got #{data.encoding})"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_decrypt_date(data, ctx)
end

#decrypt_i64(data, ctx) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/enquo/field.rb', line 53

def decrypt_i64(data, ctx)
	unless data.is_a?(String)
		raise ArgumentError, "Enquo::Field#decrypt_i64 can only decrypt from a string (got #{data.class})"
	end

	unless data.encoding == Encoding::UTF_8 && data.valid_encoding?
		raise ArgumentError, "Enquo::Field#decrypt_i64 can only decrypt validly-encoded UTF-8 strings (got #{data.encoding})"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_decrypt_i64(data, ctx)
end

#decrypt_text(data, ctx) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/enquo/field.rb', line 121

def decrypt_text(data, ctx)
	unless data.is_a?(String)
		raise ArgumentError, "Enquo::Field#decrypt_text can only decrypt from a string (got #{data.class})"
	end

	unless data.encoding == Encoding::UTF_8 && data.valid_encoding?
		raise ArgumentError, "Enquo::Field#decrypt_date can only decrypt validly-encoded UTF-8 strings (got #{data.encoding})"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_decrypt_text(data, ctx)
end

#encrypt_bool(b, ctx, safety: true, no_query: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/enquo/field.rb', line 9

def encrypt_bool(b, ctx, safety: true, no_query: false)
	unless b.is_a?(TrueClass) || b.is_a?(FalseClass)
		raise ArgumentError, "Enquo::Field#encrypt_bool can only encrypt booleans"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_encrypt_bool(b, ctx, no_query ? :no_query : safety == :unsafe ? :unsafe : :default)
end

#encrypt_date(d, ctx, safety: true, no_query: false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/enquo/field.rb', line 69

def encrypt_date(d, ctx, safety: true, no_query: false)
	unless d.is_a?(Date)
		raise ArgumentError, "Enquo::Field#encrypt_date can only encrypt Dates"
	end

	unless d.year >= -2 ** 15 && d.year < 2 ** 15 - 1
		raise RangeError, "Enquo::Field#encrypt_date can only encrypt dates where the year is between -32,768 and 32,767 (got #{d.year})"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_encrypt_date(d.year, d.month, d.day, ctx, no_query ? :no_query : safety == :unsafe ? :unsafe : :default)
end

#encrypt_i64(i, ctx, safety: true, no_query: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/enquo/field.rb', line 37

def encrypt_i64(i, ctx, safety: true, no_query: false)
	unless i.is_a?(Integer)
		raise ArgumentError, "Enquo::Field#encrypt_i64 can only encrypt integers"
	end

	unless i >= -2 ** 63 || i < 2 ** 63
		raise ArgumentError, "Enquo::Field#encrypt_i64 can only encrypt integers between -2^63 and 2^63-1 (got #{i})"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_encrypt_i64(i, ctx, no_query ? :no_query : safety == :unsafe ? :unsafe : :default)
end

#encrypt_text(t, ctx, safety: true, no_query: false) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/enquo/field.rb', line 101

def encrypt_text(t, ctx, safety: true, no_query: false)
	unless t.is_a?(String)
		raise ArgumentError, "Enquo::Field#encrypt_string can only encrypt Strings"
	end

	unless t.encoding == Encoding::UTF_8
		raise ArgumentError, "Enquo::Field#encrypt_string can only encrypt UTF-8 strings (got a string encoding of #{t.encoding})"
	end

	unless t.valid_encoding?
		raise ArgumentError, "Enquo::Field#encrypt_string can only encrypt validly-encoded UTF-8 strings"
	end

	unless ctx.is_a?(String)
		raise ArgumentError, "Encryption context must be a string (got a #{ctx.class})"
	end

	_encrypt_text(t, ctx, no_query ? :no_query : safety == :unsafe ? :unsafe : :default)
end