Skip to content

Instantly share code, notes, and snippets.

@rastislavcore
Created March 1, 2024 10:46
Show Gist options
  • Save rastislavcore/23f5e668fb6049042e4177e59de4c240 to your computer and use it in GitHub Desktop.
Save rastislavcore/23f5e668fb6049042e4177e59de4c240 to your computer and use it in GitHub Desktop.
Ican Validator on Windows with PowerShell

ICAN Validator for PowerShell

Key Features of the PowerShell Script:

  • String Manipulation: PowerShell easily handles string manipulations like converting to uppercase and removing spaces.
  • Regular Expression Support: PowerShell supports complex regular expressions, making it suitable for validating the ICAN address format.
  • Handling Large Numbers: By leveraging [System.Numerics.BigInteger], PowerShell can handle the large numbers resulting from converting the ICAN address to its numeric equivalent for the mod 97 check, overcoming the limitations present in batch scripts.
  • Simplified Syntax: PowerShell's syntax is more readable and concise for tasks like substring extraction and for-loop iterations compared to batch scripting.

Running the Script:

To execute this PowerShell script, you might need to adjust your execution policy settings if you encounter any restrictions. You can do this by running PowerShell as an Administrator and executing:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Choose "Yes" or "A" (Yes to All) when prompted to change the policy. This policy setting allows scripts to run on your system. Then, navigate to the directory containing your script and run it with:

.\ican-validator.ps1

This script will perform the ICAN address validation.

# Ask for ICAN Address input
$ICAN_ADDRESS = Read-Host "Enter ICAN Address for validation"
$CLEANED_ICAN = $ICAN_ADDRESS.ToUpper().Replace(" ", "")
# Validate format
if (-not $CLEANED_ICAN -match '^(CB|CE|AB)[0-9]{2}[A-F0-9]{40}$') {
Write-Host "Invalid ICAN Address format!"
exit 1
}
# Extract the country code, checksum, and main address part
$COUNTRY_CODE = $CLEANED_ICAN.Substring(0,2)
$CHECKSUM = $CLEANED_ICAN.Substring(2,2)
$ADDRESS_PART = $CLEANED_ICAN.Substring(4)
# Reorder: put ADDRESS_PART first, then COUNTRY_CODE, then CHECKSUM
$REORDERED_ICAN = "$ADDRESS_PART$COUNTRY_CODE$CHECKSUM"
# Convert each character to its numeric equivalent
$SUM = ""
for ($i = 0; $i -lt $REORDERED_ICAN.Length; $i++) {
$CHAR = $REORDERED_ICAN[$i]
if ($CHAR -match '[0-9]') {
$SUM += $CHAR
} else {
# Convert A-F to 10-15
$DECIMAL_VALUE = [int][char]$CHAR - 55
$SUM += $DECIMAL_VALUE
}
}
# Perform mod 97 check using BigInteger for handling large numbers
$BIG_INT = New-Object System.Numerics.BigInteger
$BIG_INT = [System.Numerics.BigInteger]::Parse($SUM)
if ($BIG_INT % 97 -eq 1) {
Write-Host "Valid ICAN Address."
exit 0
} else {
Write-Host "Invalid ICAN Address!"
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment