Created
January 30, 2020 20:46
-
-
Save SQLDBAWithABeard/f61193201ffc4387d15fbb858cd88985 to your computer and use it in GitHub Desktop.
Because Bob is awesome - Even my wife says so
This file contains hidden or 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
{ | |
"metadata": { | |
"kernelspec": { | |
"name": "powershell", | |
"display_name": "PowerShell" | |
}, | |
"language_info": { | |
"name": "powershell", | |
"codemirror_mode": "shell", | |
"mimetype": "text/x-sh", | |
"file_extension": ".ps1" | |
} | |
}, | |
"nbformat_minor": 2, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"I used the functions that I found here (Because I know Oyvind ;-) )\r\n", | |
"\r\n", | |
"https://communary.net/2016/02/19/the-luhn-algorithm/" | |
], | |
"metadata": { | |
"azdata_cell_guid": "f0836d63-90a0-443a-a7da-6b234284220e" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# first we need to run this block to load the functions for the rest of the notebook. There will be no output \r\n", | |
"function Get-LuhnChecksum {\r\n", | |
" <#\r\n", | |
" .SYNOPSIS\r\n", | |
" Calculate the Luhn checksum of a number.\r\n", | |
" .DESCRIPTION\r\n", | |
" The Luhn algorithm or Luhn formula, also known as the \"modulus 10\" or \"mod 10\" algorithm, \r\n", | |
" is a simple checksum formula used to validate a variety of identification numbers, such as \r\n", | |
" credit card numbers, IMEI numbers, National Provider Identifier numbers in the US, and \r\n", | |
" Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn.\r\n", | |
" .EXAMPLE\r\n", | |
" Get-LuhnChecksum -Number 1234567890123452\r\n", | |
" Calculate the Luch checksum of the number. The result should be 60.\r\n", | |
" .INPUTS\r\n", | |
" System.UInt64\r\n", | |
" .NOTES\r\n", | |
" Author: Øyvind Kallstad\r\n", | |
" Date: 19.02.2016\r\n", | |
" Version: 1.0\r\n", | |
" Dependencies: ConvertTo-Digits\r\n", | |
" .LINKS\r\n", | |
" https://en.wikipedia.org/wiki/Luhn_algorithm\r\n", | |
" https://communary.wordpress.com/\r\n", | |
" https://github.com/gravejester/Communary.ToolBox\r\n", | |
" #>\r\n", | |
" [CmdletBinding()]\r\n", | |
" param (\r\n", | |
" [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]\r\n", | |
" [ValidateNotNullOrEmpty()]\r\n", | |
" [uint64] $Number\r\n", | |
" )\r\n", | |
"\r\n", | |
" $digitsArray = ConvertTo-Digits -Number $Number\r\n", | |
" [array]::Reverse($digitsArray)\r\n", | |
"\r\n", | |
" $sum = 0\r\n", | |
" $index = 0\r\n", | |
"\r\n", | |
" foreach ($digit in $digitsArray) {\r\n", | |
" if (($index % 2) -eq 0) {\r\n", | |
" $doubledDigit = $digit * 2\r\n", | |
" if (-not($doubledDigit -eq 0)) {\r\n", | |
" $doubleDigitArray = ConvertTo-Digits -Number $doubledDigit\r\n", | |
" $sum += ($doubleDigitArray | Measure-Object -Sum | Select-Object -ExpandProperty Sum)\r\n", | |
" }\r\n", | |
" }\r\n", | |
" else {\r\n", | |
" $sum += $digit\r\n", | |
" }\r\n", | |
" $index++\r\n", | |
" }\r\n", | |
" Write-Output $sum\r\n", | |
"}\r\n", | |
"\r\n", | |
"function New-LuhnChecksumDigit {\r\n", | |
" <#\r\n", | |
" .SYNOPSIS\r\n", | |
" Calculate the Luhn checksum digit for a number.\r\n", | |
" .DESCRIPTION\r\n", | |
" This function uses the Luhn algorithm to calculate the\r\n", | |
" Luhn checksum digit for a (partial) number.\r\n", | |
" .EXAMPLE\r\n", | |
" New-LuhnChecksumDigit -PartialNumber 123456789012345\r\n", | |
" This will get the checksum digit for the number. The result should be 2.\r\n", | |
" .INPUTS\r\n", | |
" System.UInt64\r\n", | |
" .NOTES\r\n", | |
" Author: Øyvind Kallstad\r\n", | |
" Date: 19.02.2016\r\n", | |
" Version: 1.0\r\n", | |
" Dependencies: Get-LuhnCheckSum\r\n", | |
" .LINKS\r\n", | |
" https://en.wikipedia.org/wiki/Luhn_algorithm\r\n", | |
" https://communary.wordpress.com/\r\n", | |
" https://github.com/gravejester/Communary.ToolBox\r\n", | |
" #>\r\n", | |
" [CmdletBinding()]\r\n", | |
" param (\r\n", | |
" [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]\r\n", | |
" [uint64] $PartialNumber\r\n", | |
" )\r\n", | |
"\r\n", | |
" $checksum = Get-LuhnCheckSum -Number $PartialNumber\r\n", | |
" Write-Output (($checksum * 9) % 10)\r\n", | |
"}\r\n", | |
"\r\n", | |
"function Test-IsLuhnValid {\r\n", | |
" <#\r\n", | |
" .SYNOPSIS\r\n", | |
" Valdidate a number based on the Luhn Algorithm.\r\n", | |
" .DESCRIPTION\r\n", | |
" This function uses the Luhn algorithm to validate a number that includes\r\n", | |
" the Luhn checksum digit.\r\n", | |
" .EXAMPLE\r\n", | |
" Test-IsLuhnValid -Number 1234567890123452\r\n", | |
" This will validate whether the number is valid according to the Luhn Algorithm.\r\n", | |
" .INPUTS\r\n", | |
" System.UInt64\r\n", | |
" .OUTPUTS\r\n", | |
" System.Boolean\r\n", | |
" .NOTES\r\n", | |
" Author: Øyvind Kallstad\r\n", | |
" Date: 19.02.2016\r\n", | |
" Version: 1.0\r\n", | |
" Dependencies: Get-LuhnCheckSum, ConvertTo-Digits\r\n", | |
" .LINKS\r\n", | |
" https://en.wikipedia.org/wiki/Luhn_algorithm\r\n", | |
" https://communary.wordpress.com/\r\n", | |
" https://github.com/gravejester/Communary.ToolBox\r\n", | |
" #>\r\n", | |
" [CmdletBinding()]\r\n", | |
" param (\r\n", | |
" [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]\r\n", | |
" [uint64] $Number\r\n", | |
" )\r\n", | |
"\r\n", | |
" $numberDigits = ConvertTo-Digits -Number $Number\r\n", | |
" $checksumDigit = $numberDigits[-1]\r\n", | |
" $numberWithoutChecksumDigit = $numberDigits[0..($numberDigits.Count - 2)] -join ''\r\n", | |
" $checksum = Get-LuhnCheckSum -Number $numberWithoutChecksumDigit\r\n", | |
"\r\n", | |
" if ((($checksum + $checksumDigit) % 10) -eq 0) {\r\n", | |
" Write-Output $true\r\n", | |
" }\r\n", | |
" else {\r\n", | |
" Write-Output $false\r\n", | |
" }\r\n", | |
"}\r\n", | |
"function ConvertTo-Digits {\r\n", | |
" <#\r\n", | |
" .SYNOPSIS\r\n", | |
" Convert an integer into an array of bytes of its individual digits.\r\n", | |
" .DESCRIPTION\r\n", | |
" Convert an integer into an array of bytes of its individual digits.\r\n", | |
" .EXAMPLE\r\n", | |
" ConvertTo-Digits 145\r\n", | |
" .INPUTS\r\n", | |
" System.UInt64\r\n", | |
" .LINK\r\n", | |
" https://communary.wordpress.com/\r\n", | |
" https://github.com/gravejester/Communary.ToolBox\r\n", | |
" .NOTES\r\n", | |
" Author: Øyvind Kallstad\r\n", | |
" Date: 09.05.2015\r\n", | |
" Version: 1.0\r\n", | |
" #>\r\n", | |
" [OutputType([System.Byte[]])]\r\n", | |
" [CmdletBinding()]\r\n", | |
" param(\r\n", | |
" [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]\r\n", | |
" [uint64]$Number\r\n", | |
" )\r\n", | |
" $n = $Number\r\n", | |
" $numberOfDigits = 1 + [convert]::ToUInt64([math]::Floor(([math]::Log10($n))))\r\n", | |
" $digits = New-Object Byte[] $numberOfDigits\r\n", | |
" for ($i = ($numberOfDigits - 1); $i -ge 0; $i--) {\r\n", | |
" $digit = $n % 10\r\n", | |
" $digits[$i] = $digit\r\n", | |
" $n = [math]::Floor($n / 10)\r\n", | |
" }\r\n", | |
" Write-Output $digits\r\n", | |
"}" | |
], | |
"metadata": { | |
"azdata_cell_guid": "249ee1cc-2303-4441-b523-ed0bcec551d5", | |
"tags": [ | |
"hide_input" | |
] | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"text": "", | |
"output_type": "stream" | |
} | |
], | |
"execution_count": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"With those loaded we can now run our code" | |
], | |
"metadata": { | |
"azdata_cell_guid": "26f5ef8f-2d72-4269-b7b3-7d22063a8923" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Test-IsLuhnValid -NUmber 1114" | |
], | |
"metadata": { | |
"azdata_cell_guid": "fa68278d-4c59-4411-9f99-fc92365c7648" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"text": "False\n", | |
"output_type": "stream" | |
} | |
], | |
"execution_count": 8 | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment