##########################################################
# ULA を生成する(RFC4193)
##########################################################
function GenerateULA(){
# NTP Time 求める
$NtpTime = [long](New-TimeSpan 1900/01/01).TotalSeconds
$HexNTPTime = $NtpTime.ToString("x")
# MAC アドレス求める
$NICs = @(Get-NetAdapter | ?{$_.MediaType -eq "802.3"})
$MAC = $NICs[0].MacAddress
# MAC アドレスを EUI-64 にする
$MAC = $MAC.Replace("-","")
$HiMAC1 = $MAC.Substring(0,2)
$hiMAC2 = $MAC.Substring(2,4)
$LoMAC3 = $MAC.Substring(6,6)
$numHiMAC1 = [Convert]::ToInt32($HiMAC1,16)
$XorMask = [Convert]::ToInt32("00000010",2)
$numHiMAC1 = $numHiMAC1 -bxor $XorMask
$HiMAC1 = $numHiMAC1.ToString("x")
$EUI64 = $HiMAC1 + $HiMAC2 + "fffe" + $LoMAC3
# NTP Time と EUI-64 を連結する
$BaseString = $HexNTPTime + $EUI64
# SHA-1 ハッシュを求める
$ByteDatas = [System.Text.Encoding]::UTF8.GetBytes($BaseString)
Add-Type -AssemblyName System.Security
$SHA1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
$HashBytes = $SHA1.ComputeHash($ByteDatas)
$SHA1.Dispose()
$strHash = ""
foreach( $HashByte in $HashBytes ){
$strHash += $HashByte.ToString("x2")
}
# ラスト10文字を切り取り fd をつける
$ULA_Bear = "fd" + $strHash.Substring(30,10)
# IPv6 表記にする
$ULA1 = $ULA_Bear.Substring(0,4)
$ULA2 = $ULA_Bear.Substring(4,4)
$ULA3 = $ULA_Bear.Substring(8,4)
$ULA = $ULA1 + ":" + $ULA2 + ":" + $ULA3 + "::/48"
return $ULA
}
|