drop function if exists "public"."hashed_text_string"(hashed hashed_text);

drop type "public"."email";

drop type "public"."hashed_text";

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.email_of_string(val text)
 RETURNS email
 LANGUAGE sql
AS $function$select row(val);$function$
;

CREATE OR REPLACE FUNCTION public.email_to_string(val email)
 RETURNS text
 LANGUAGE sql
AS $function$select (val.str);$function$
;

CREATE OR REPLACE FUNCTION public.hashed_text_of_string(val text)
 RETURNS hashed_text
 LANGUAGE sql
AS $function$select row(val);$function$
;

CREATE OR REPLACE FUNCTION public.hashed_text_to_string(val hashed_text)
 RETURNS text
 LANGUAGE sql
AS $function$select (val.str);$function$
;

create type "public"."email" as ("str" text);

CREATE OR REPLACE FUNCTION public.hash_text(plain text)
 RETURNS hashed_text
 LANGUAGE plpgsql
 IMMUTABLE
AS $function$
begin
  return hashed_text_of_string(crypt(plain, gen_salt('bf')));
end;
$function$
;

create type "public"."hashed_text" as ("str" text);

CREATE OR REPLACE FUNCTION public.hashed_text_matches(plain text, hashed hashed_text)
 RETURNS boolean
 LANGUAGE plpgsql
 IMMUTABLE
AS $function$
begin
 return hashed_text_to_string(hashed) = crypt(plain, hashed_text_to_string(hashed));
end;
$function$
;


