powershell.net-corebigintegerpowershell-v6.0

Powershell bigint output differ from declared value


I'm porting lesspass password manager to powershell, but I'm having trouble when implementing the _consume_entropy() method, especially the Python divmod.

Reproduce

PS> [bigint]$EntropyAsInt = 99600400399777174105034830393873797761817714609490038944205586760025858632478
PS> Write-Host $EntropyAsInt
99600400399777173995117538344184441997741701018199539534149245151907290284032

Difference

99600400399777174105034830393873797761817714609490038944205586760025858632478
99600400399777173995117538344184441997741701018199539534149245151907290284032
                ^ start diverging he

Question

What's the matter here? Am I using the wrong type?


Solution

  • 99600400399777174105034830393873797761817714609490038944205586760025858632478 is a [Double]...

    PS> 99600400399777174105034830393873797761817714609490038944205586760025858632478
    9.96004003997772E+76
    PS> (99600400399777174105034830393873797761817714609490038944205586760025858632478).ToString("F0")
    99600400399777200000000000000000000000000000000000000000000000000000000000000
    PS> (99600400399777174105034830393873797761817714609490038944205586760025858632478).GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Double                                   System.ValueType
    

    By the time it gets cast to [BigInt] the original value is already lost. Starting with a [String] preserves it, though...

    PS> [bigint]$EntropyAsInt = '99600400399777174105034830393873797761817714609490038944205586760025858632478'
    PS> $EntropyAsInt
    99600400399777174105034830393873797761817714609490038944205586760025858632478
    

    See Instantiating a BigInteger Object for more information.