-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplePseudonym.sh
executable file
·88 lines (73 loc) · 1.89 KB
/
SimplePseudonym.sh
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
#!/bin/bash
SEP=","
PSEUDONYMS="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/pseudonyms.csv"
function getRandom() {
echo "$(dd if=/dev/urandom bs=1M count=1 status=none | sha1sum | grep -oE '^\w+')"
}
function _pseudomize() {
if [ ! -f "$PSEUDONYMS" ]; then
touch "$PSEUDONYMS"
fi
(
flock 42
PSN="$(grep -oP '(?<=^'"$1$SEP"')\w+$' "$PSEUDONYMS")"
if [ $? -gt 0 ]; then
PSN="$(getRandom)"
grep -qP '^\w+(?='"$SEP$PSN"'$)' "$PSEUDONYMS"
while [ $? -eq 0 ]; do
PSN="$(getRandom)"
grep -qP '^\w+(?='"$SEP$PSN"'$)' "$PSEUDONYMS"
done;
echo "$1$SEP$PSN" >> pseudonyms.csv
fi
echo "$PSN"
) 42<"$PSEUDONYMS"
}
function _depseudomize() {
if [ ! -f "$PSEUDONYMS" ]; then
touch "$PSEUDONYMS"
fi
grep -oP '^\w+(?='"$SEP$1"'$)' "$PSEUDONYMS" || echo "UNKOWN PSEUDONYM: $1"
}
function pseudomize() {
case $1 in
-)
while read line; do
_pseudomize "$line"
done </dev/stdin ;;
*)
_pseudomize "$1" ;;
esac
}
function depseudomize() {
case $1 in
-)
while read line; do
_depseudomize "$line"
done </dev/stdin ;;
*)
_depseudomize "$1" ;;
esac
}
TEMP=`getopt -o hp:d: --long help,pseudomize:,depseudomize: -n "$(basename "$BASH_SOURCE")" -- "${@}"`
if [ $? != 0 ] ; then exit 1 ; fi
eval set -- "${TEMP}";
while [[ ${1:0:1} = - ]]; do
case $1 in
-h|--help)
cat <<EOF
$(basename "$BASH_SOURCE") translates identifier to random pseudonyms and reverse.
USAGE: $(basename "$BASH_SOURCE") [OPTIONS]
OPTIONS
-h --help Print this help
-p --pseudomize Get pseudonym for identifier (use -p- to read list from STDIN)
-d --depseudomize Get identifier for pseudonym (use -d- to read list from STDIN)
EOF
shift 1; exit ;;
--) shift 1; break ;;
-p|--pseudomize) pseudomize "$2"; shift 2; continue ;;
-d|--depseudomize) depseudomize "$2"; shift 2; continue ;;
esac
echo "ERROR: Unknown parameter ${1}"
exit;
done