Skip to content

Instantly share code, notes, and snippets.

@kaimi
Last active December 31, 2015 12:09
Show Gist options
  • Save kaimi/7984644 to your computer and use it in GitHub Desktop.
Save kaimi/7984644 to your computer and use it in GitHub Desktop.
IBAN-Rechner für deutsche Konten
#!/usr/bin/env bash
#
# berechnet zu einer gegebenen deutschen Bankleitzahl-/Kontonummer-Kombination
# die zugehörige IBAN
#
# !ACHTUNG! man beachte
# https://de.wikipedia.org/wiki/International_Bank_Account_Number#Generierung_der_IBAN
# Alle Nummern ohne Gewähr!
#
blz=$1
knummer=$2
psumme="00"
if [[ ! ( "${blz}${knummer}" =~ ^[0-9]+$ ) ]] ; then
echo "BLZ/Kontonummer müssen schon Zahlen sein, mein Lieber :)" >&2
exit 1
fi
# padding
len=${#knummer}
for (( i=0; i<10-len; i++ ))
do
knummer="0${knummer}"
done
# prüfsumme
# https://de.wikipedia.org/wiki/International_Bank_Account_Number#Berechnung_der_Pr.C3.BCfsumme
psumme=$(expr 98 - "${blz}${knummer}1314${psumme}" % 97)
if [ ${#psumme} -ne 2 ]
then
psumme="0${psumme}"
fi
# validierung (sicher ist sicher!)
# https://de.wikipedia.org/wiki/International_Bank_Account_Number#Validierung_der_Pr.C3.BCfsumme
check=$(expr "${blz}${knummer}1314${psumme}" % 97)
if [ $check -eq 1 ]
then
echo "DE${psumme}${blz}${knummer}"
else
echo "Fehler bei der Berechnung. L2code! :-/" >&2
fi
@xypron
Copy link

xypron commented May 15, 2015

On my Debain 8.0 system bash does not support 128bit integer calculation. So I separate the modulus calculation into multiple steps.
none of the numbers may have leading 0s at this would lead to the number be considered octal.
Hence the expansion of the account nummber (knummer) should be done after the modulus calculation.

#!/bin/bash
# 
# berechnet zu einer gegebenen deutschen Bankleitzahl-/Kontonummer-Kombination 
# die zugehoerige IBAN
#
# !ACHTUNG! man beachte
# https://de.wikipedia.org/wiki/International_Bank_Account_Number#Generierung_der_IBAN
# Alle Nummern ohne Gewaehr!
#

blz=$1
knummer=$2

if [[ ! ( "${blz}${knummer}" =~ ^[0-9]+$ ) ]] ; then
  echo "BLZ/Kontonummer duerfen nur Ziffern enthalten" >&2
  exit 1
fi

psumme=$(( $blz % 97 ))
psumme=$(( $knummer % 97 + $psumme * (10000000000 % 97)))
psumme=$(( 1314 % 97 + $psumme * (10000 % 97)))
psumme=$(( $psumme * (100 % 97)))
psumme=$(( 98 - ($psumme % 97)))

# padding
len=${#knummer}
for (( i=0; i<10-len; i++ ))
do
knummer="0${knummer}"
done 

if [ ${#psumme} -ne 2 ]
then
psumme="0${psumme}"
fi

echo "DE${psumme}${blz}${knummer}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment