Last active
March 29, 2022 01:58
-
-
Save TheKevinWang/39091c61a286e2ca801537bfa5062474 to your computer and use it in GitHub Desktop.
Quick and dirty script to generate a random IMEI from an existing one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# Takes an IMEI number, randomizes the SNR (unique identifier), calculates the check digit using Luhn algorithm, and returns the resulting IMEI. | |
#> | |
function Generate-IMEI([string] $seed) { | |
#generate random SNR | |
$str = ($seed.substring(0,8)+ [string](Get-Random -max 999999 -min 0) + $seed[14]) | |
$ca = $str.ToCharArray() | |
$sum = 0 | |
#calculate check digit | |
for($i=0; $i -lt $ca.length-1; $i++) { | |
$digit = [int]([string]$ca[$i]) | |
if(($i % 2) -ne 0) { | |
$digit = $digit*2 | |
} | |
#sum up digits, ie 32 => 3+2 = 5 | |
[int[]](($digit -split '') -ne '') | %{$sum += $_}; | |
} | |
return ($str.substring(0,14)+([math]::ceiling($sum/10)*10 - $sum)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment