Repository navigation
Expand file tree
/
Copy pathhash-function.sql
More file actions
154 lines (120 loc) · 3.34 KB
/
Copy pathhash-function.sql
File metadata and controls
154 lines (120 loc) · 3.34 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
set serveroutput on size unlimited
alter session set plsql_ccflags = 'unit_test:true, develop:true';
create or replace package hash
is
function md5_hash( clobIn in out nocopy clob) return CLOB;
function sha256_hash( clobIn in out nocopy clob) return CLOB;
function sha512_hash( clobIn in out nocopy clob) return CLOB;
$IF $$unit_test $THEN
procedure unit_test ;
$END
end;
/
show errors package hash
create or replace package body hash
is
$IF $$unit_test $THEN
cTestData clob;
cDigest clob;
$END
/*
====================================================================
== Available Hash Types
==
== HASH_MD4 CONSTANT PLS_INTEGER := 1;
== HASH_MD5 CONSTANT PLS_INTEGER := 2;
== HASH_SH1 CONSTANT PLS_INTEGER := 3;
== HASH_SH256 CONSTANT PLS_INTEGER := 4;
== HASH_SH384 CONSTANT PLS_INTEGER := 5;
== HASH_SH512 CONSTANT PLS_INTEGER := 6;
====================================================================
*/
/*
===============================================
== function digest
== return a Hash based on CLOB passed
===============================================
*/
function digest(hashTypeIn PLS_INTEGER, clobIn in out nocopy clob) return CLOB
is
hashVal CLOB;
begin
hashVal := rawtohex(dbms_crypto.hash(src => clobIn, typ =>hashTypeIn ));
return hashVal;
exception
when others then
-- error code here if desired
raise;
end;
/*
===============================================
== function md5_digest
== return an MD5 Hash based on CLOB passed
===============================================
*/
function md5_hash( clobIn in out nocopy clob) return CLOB
is
hashVal CLOB;
begin
hashVal := digest(hashTypeIn => dbms_crypto.hash_md5, clobIn => clobIn);
return hashVal;
end;
/*
===============================================
== function sha256_digest
== return an SHA256 Hash based on CLOB passed
===============================================
*/
function sha256_hash( clobIn in out nocopy clob) return CLOB
is
hashVal CLOB;
begin
hashVal := digest(hashTypeIn => dbms_crypto.hash_sh256, clobIn => clobIn);
return hashVal;
end;
/*
===============================================
== function sha512_digest
== return an SHA512 Hash based on CLOB passed
===============================================
*/
function sha512_hash( clobIn in out nocopy clob) return CLOB
is
hashVal CLOB;
begin
hashVal := digest(hashTypeIn => dbms_crypto.hash_sh512, clobIn => clobIn);
return hashVal;
end;
$IF $$unit_test $THEN
/*
===============================================
== when $$unit_test is true just run this
== exec hash.main
===============================================
*/
procedure unit_test
is
begin
dbms_output.enable(null);
cTestData := 'The Quick Brown Fox Jumped Over the Lazy Dog!';
dbms_output.put_line('Test Data: ' || cTestData);
cDigest := md5_hash(cTestData);
dbms_output.put_line('MD5 Digest: ' || cDigest);
cDigest := sha256_hash(cTestData);
dbms_output.put_line('SHA256 Digest: ' || cDigest);
cDigest := sha512_hash(cTestData);
dbms_output.put_line('SHA512 Digest: ' || cDigest);
end;
$END
begin
null;
end;
/
show errors package body hash
begin
$IF $$unit_test $THEN
hash.unit_test;
$END
null;
end;
/