Skip to content

Instantly share code, notes, and snippets.

@behnampro
Last active February 16, 2021 15:49
Show Gist options
  • Save behnampro/bce0021ff2b31e2c2ce3781e5f45a82e to your computer and use it in GitHub Desktop.
Save behnampro/bce0021ff2b31e2c2ce3781e5f45a82e to your computer and use it in GitHub Desktop.
تشخیص صحت کد شبا در گولنگ | validate IR sheba code
func CheckSheba(text string) bool {
matched, err := regexp.MatchString("^IR[0-9]{24}$", text)
if err != nil || matched != true {
return false
}
firstForChars := ""
sheba := ""
for index := range text {
if index < 4 {
if string(text[index]) == "I" {
firstForChars = firstForChars + "18"
} else if string(text[index]) == "R" {
firstForChars = firstForChars + "27"
} else {
firstForChars = firstForChars + string(text[index])
}
} else {
sheba = sheba + string(text[index])
}
}
sheba = sheba + firstForChars
bigNum97 := big.NewInt(97)
n := new(big.Int)
shebaNumber, ok := n.SetString(sheba, 10)
if !ok {
return false
}
bigNum1 := new(big.Int)
bigNum1.SetInt64(1)
return 0 == bigNum1.Cmp(shebaNumber.Mod(shebaNumber, bigNum97))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment