-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathccv.sh
More file actions
executable file
·107 lines (106 loc) · 2.26 KB
/
ccv.sh
File metadata and controls
executable file
·107 lines (106 loc) · 2.26 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
#!/bin/bash
checkprovider(){
str=$1 #grab the string
fdig=$(echo ${str:0:1}) #pull the first digit to check provider of card
case "$fdig" in
4)
provider="Visa"
;;
5)
provider="Mastercard"
;;
6)
provider="Discover"
;;
3)
provider="American Express"
;;
*)
provider="Unknown Provider"
esac
echo "Entered CC number: $str" #display number
echo "Entered CC's provider: $provider" #display provider
}
getchecknumber(){ # get the last digit of the cc number
str=$1
checknum=$(echo ${str: -1})
echo $checknum
}
getrest(){ # get the rest of the numbers in the sequence
str=$(echo ${str%?})
}
doubleeveryother(){ # double every other number
# split characters into an array
arr=()
i=0
while [ "$i" -lt "${#str}" ]; do
arr+=(${str:$i:1})
i=$((i+1))
done
for ((i=1; i<=15; i++))
do {
char=$(echo "$i-1" | bc)
double=$(echo "${arr[$char]} * 2" | bc)
if [ $(($i%2)) -eq 0 ] ; then
printf "${arr[$char]} "
else
arr[$char]=$double #replace the number in the array
echo -n "$double "
fi
}
done
echo -e
}
adddigitsum() { #split a two digit number into two numbers and add them
for ((a=0; a<=14; a++))
do {
NUMSPLIT=()
number=$(echo ${arr[$a]})
if [[ ${#number} == 2 ]] ; then
i=0
while [[ "$i" -lt ${#number} ]]; do
NUMSPLIT+=(${number:$i:1})
i=$((i+1))
done
num=$((${NUMSPLIT[0]} + ${NUMSPLIT[1]}))
addeddigit=$( echo $num | bc )
arr[$a]=$addeddigit # replace the number in the array
echo -n "$addeddigit "
fi
}
done
}
addall(){ #add all the digits
sum=$( IFS="+"; bc <<< "${arr[*]}" )
echo -e "$sum"
}
multiply(){ #multiply by 9
result=$( echo "$sum * 9" | bc )
echo $result
}
validate(){ #see if the last number of 'result' is equal to the checknum
resultchecknum=$(echo ${result: -1})
if [[ $resultchecknum == $checknum ]] ; then
echo "$checknum = $resultchecknum"
echo "CC Number is valid!"
else
echo "$checknum ≠ $resultchecknum"
echo "CC Number is not valid."
fi
}
checkprovider $1
echo -e
echo "Getting check number..."
getchecknumber $1
echo "Getting the rest of the sequence..."
getrest
echo "Doubling every other number..."
doubleeveryother
echo "Adding two digit sums..."
adddigitsum
echo -e "\nGetting sums..."
addall
echo "Multiplying..."
multiply
echo "Validating..."
validate