BAMCIS.ByteArrays.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
Function Set-ByteArrayPadding {
    <#
        .SYNOPSIS
            Pads an array with a leading byte so that the resulting array is the length specified.
 
        .DESCRIPTION
            This cmdlet takes an input array, creates a new array initialized with the padding byte specified, and
            then copies the supplied input array to the end of the new array. So if the array @(0x01, 0x02) was supplied with a padding
            byte of 0x03 and a length of 4, the resulting array would be @(0x03, 0x03, 0x01, 0x02).
 
            The original input array is unchanged and the resulting data is a new object returned to the pipeline if used with InputObject or supplied
            via the pipeline. The original array can be modified by use of the ReferenceObject parameter.
 
        .PARAMETER InputObject
            The array that needs to be padded.
 
        .PARAMETER ReferenceObject
            A reference to the array that needs to be padded. If this parameter is supplied, no output is returned to the pipeline
 
        .PARAMETER Length
            The desired length of the resulting array.
 
        .PARAMETER Padding
            The byte value used to pad the left hand side of the array. This defaults to 0x00.
 
        .PARAMETER PadEnd
            Instead of padding the left side of the array, the right side of the array is padded, so for an input of @(0x01) and a lenght of 4,
            the resulting array would be @(0x01, 0x00, 0x00, 0x00).
 
        .EXAMPLE
            $Arr = @(0x01)
            $Arr = $Arr | Set-ByteArrayPadding -Length 4
 
            The resulting array in $Arr is length 4 and its contents are: @(0x00, 0x00, 0x00, 0x01)
 
        .EXAMPLE
            $Arr = @(0x01)
            Set-ByteArrayPadding ([ref]$Arr) -Length 2 -Padding 0xFF -PadEnd
 
            The variable $Arr is modified by reference so that the original variable's contents after the cmdlet are @(0x01, 0xFF). Here the array
            is padded on the end with the specified 0xFF byte.
 
        .INPUTS
            System.Byte[]
 
        .OUTPUTS
            None or System.Byte[]
 
        .NOTES
            AUTHOR: Michael Haken
            LAST UPDATE: 1/19/2018
    #>

    [CmdletBinding()]
    [OutputType([System.Byte[]])]
    Param(
        [Parameter(Position = 0, ValueFromPipeline = $true, ParameterSetName = "Input")]
        [ValidateNotNull()]
        [System.Byte[]]$InputObject = @(),

        [Parameter(Position = 0, ParameterSetName = "Ref", Mandatory = $true)]
        [Ref]$ReferenceObject,

        [Parameter(Mandatory = $true, Position = 1)]
        [System.Int32]$Length,

        [Parameter(Position = 3)]
        [ValidateNotNull()]
        [System.Byte]$Padding = 0x00,

        [Parameter()]
        [Switch]$PadEnd
    )

    Begin {

    }

    Process {
        
        switch ($PSCmdlet.ParameterSetName)
        {
            "Input" {
                if ($InputObject.Length -lt $Length)
                {
                    [System.Byte[]]$NewData = [System.Byte[]](,$Padding * $Length)

                    [System.Int32]$CopyStartIndex = $Length - $InputObject.Length

                    if ($PadEnd)
                    {
                        $CopyStartIndex = 0
                    }

                    # Copy the original data to the new data array starting at the padding count index
                    # which will pad the new array with leading padding bytes
            
                    [System.Array]::Copy($InputObject, 0, $NewData, $CopyStartIndex, $InputObject.Length)

                    $InputObject = $NewData
                }

                Write-Output -InputObject $InputObject

                break
            }
            "Ref" {
                if ($ReferenceObject.Value.Length -lt $Length)
                {
                    [System.Byte[]]$NewData = [System.Byte[]](,$Padding * $Length)

                    [System.Int32]$CopyStartIndex = $Length - $ReferenceObject.Value.Length

                    if ($PadEnd)
                    {
                        $CopyStartIndex = 0
                    }

                    [System.Array]::Copy($ReferenceObject.Value, 0, $NewData, $CopyStartIndex, $ReferenceObject.Value.Length)

                    $ReferenceObject.Value = $NewData
                }

                break
            }
            default {
                Write-Error -Exception (New-Object -TypeName System.ArgumentException("Unknown parameter set, $($PSCmdlet.ParameterSetName), for $($MyInvocation.MyCommand).")) -ErrorAction Stop
            }
        }      
    }

    End {
    }
}

Function Remove-ByteArrayPadding {
    <#
        .SYNOPSIS
            Removes padding from the beginning or end of a byte array.
 
        .DESCRIPTION
            This cmdlet removes the padding from the beginning or end of an array and provides a new or modified array
            with those bytes removed. The caller can specify the byte that is used as padding and provide the input array
            either through the pipeline, parameter, or by reference. When passed by reference, nothing is returned to
            the pipeline.
 
        .PARAMETER InputObject
            The byte array to remove padding from.
 
        .PARAMETER ReferenceObject
            The reference to a byte array that will have padding removed from it. The reference will point to a different
            location in memory after the cmdlet is complete if any modifications have been done.
 
        .PARAMETER FromEnd
            Specifies that padding is removed from the tail end of the array instead of the beginning.
 
        .PARAMETER Padding
            The byte character that is used as padding to be removed. This defaults to 0x00.
 
        .EXAMPLE
            $Arr = @(0x00, 0x00, 0x00, 0x01)
            Remove-ByteArrayPadding -InputObject $Arr
 
            The results of this cmdlet will produce a new array with contents @(0x01).
 
        .EXAMPLE
            $Arr = @(0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF)
            ([ref]$Arr) | Remove-ByteArrayPadding -Padding 0xFF -FromEnd
 
            This example demonstrates several things. First the input array can be passed by reference through the pipeline.
            After the cmdlet complete, the variable $Arr will contain @(0x00, 0x00, 0x00, 0x01). The cmdlet specified the padding
            character to 0xFF and it removed the padding from the end of the array instead of the beginning.
 
        .INPUTS
            System.Byte[]
 
        .OUTPUTS
            System.Byte[] or None
 
        .NOTES
            AUTHOR: Michael Haken
            LAST UPDATE: 1/22/2018
    #>

    [CmdletBinding()]
    [OutputType([System.Byte[]])]
    Param(
        [Parameter(ValueFromPipeline = $true, Position = 0, ParameterSetName = "Input")]
        [ValidateNotNull()]
        [System.Byte[]]$InputObject,

        [Parameter(ValueFromPipeline = $true, Position = 0, ParameterSetName = "Ref")]
        [ValidateNotNull()]
        [Ref]$ReferenceObject,

        [Parameter()]
        [Switch]$FromEnd,

        [Parameter()]
        [ValidateNotNull()]
        [System.Byte]$Padding = 0x00
    )

    Begin {
        [System.Byte[]]$Bytes = @()
    }

    Process {
        switch ($PSCmdlet.ParameterSetName)
        {
            "Input" {
                $Bytes += $InputObject
                break
            }
            "Ref" {
                $Bytes = $ReferenceObject.Value
                break
            }
            default {
                Write-Error -Exception (New-Object -TypeName System.ArgumentException("Unknown parameter set, $($PSCmdlet.ParameterSetName), for $($MyInvocation.MyCommand).")) -ErrorAction Stop
            }
        }
        
    }

    End {

        if ($FromEnd)
        {      
            $StartIndex = $Bytes.Length - 1

            while ($StartIndex -ge 0 -and $Bytes[$StartIndex] -eq $Padding)
            {
                $StartIndex--
            }

            if ($StartIndex -ge 0)
            {
                # Since StartIndex is the index where the first non zero byte is, add 1 to make it a length
                [System.Byte[]]$FinalBytes = New-Object -TypeName System.Byte[] -ArgumentList ($StartIndex + 1)

                [System.Array]::Copy($Bytes, 0, $FinalBytes, 0, $StartIndex + 1)

                $Bytes = $FinalBytes
            }
        }
        else
        {
            $StartIndex = 0

            while ($StartIndex -lt $Bytes.Length -and $Bytes[$StartIndex] -eq $Padding)
            {
                $StartIndex++
            }

            if ($StartIndex -lt $Bytes.Length)
            {
                [System.Byte[]]$FinalBytes = New-Object -TypeName System.Byte[] -ArgumentList ($Bytes.Length - $StartIndex)

                [System.Array]::Copy($Bytes, $StartIndex, $FinalBytes, 0, $Bytes.Length - $StartIndex)

                $Bytes = $FinalBytes
            }
        }

        switch ($PSCmdlet.ParameterSetName)
        {
            "Input" {
                Write-Output -InputObject $Bytes
                break
            }
            "Ref" {
                $ReferenceObject.Value = $Bytes
                break
            }
        }
    }
}

Function Invoke-ByteArrayTrim {
    <#
        .SYNOPSIS
            Trims a byte array to the desired length.
 
        .DESCRIPTION
            This cmdlet trims a byte array down to the specified size. By default,
            bytes are removed from the end of the array unless TrimStart is specified.
 
        .PARAMETER InputObject
            The byte array to trim.
 
        .PARAMETER DesiredLength
            The new desired length of the array. If the array is equal to or smaller in size
            than the desired length, no actions are performed and the array is returned as is.
 
        .PARAMETER TrimStart
            If specified, bytes are trimmed from the beginning of the array instead of the end.
 
        .EXAMPLE
            $B = @(0x00, 0x01, 0x02)
            $B = $B | Invoke-ByteArrayTrim -DesiredLenth 1
 
            In this example, the byte array $B is trimmed down to a length of 1 and results in an array
            containing @(0x00).
 
        .EXAMPLE
            $B = @(0x00, 0x01, 0x02)
            $B = $B | Invoke-ByteArrayTrim -DesiredLenth 2 -TrimStart
 
            In this example, the byte array $B is trimmed down to a length of 2 and results in an array
            containing @(0x01, 0x02).
 
        .INPUTS
            System.Byte[]
 
        .OUTPUTS
            System.Byte[]
 
        .NOTES
            AUTHOR: Michael Haken
            LAST UPDATE: 1/22/2018
    #>

    [CmdletBinding()]
    [OutputType([System.Byte[]])]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [ValidateNotNull()]
        [System.Byte[]]$InputObject,

        [Parameter(Mandatory = $true)]
        [System.Int32]$DesiredLength,

        [Parameter()]
        [Switch]$TrimStart
    )

    Begin{
        if ($DesiredLength -lt 0)
        {
            Write-Error -Exception (New-Object -TypeName System.Argument.Exception("The desired length cannot be less than 0.")) -ErrorAction Stop
        }

        [System.Byte[]]$Bytes = @()
    }

    Process {
        $Bytes += $InputObject
    }

    End {

        if ($DesiredLength -lt $Bytes.Length)
        {
            [System.Byte[]]$Temp = New-Object -TypeName System.Byte[] -ArgumentList $DesiredLength

            $StartIndex = 0

            if ($TrimStart)
            {
                $StartIndex = $Bytes.Length - $DesiredLength
            }

            [System.Array]::Copy($Bytes, $StartIndex, $Temp, 0, $DesiredLength)

            Write-Output -InputObject $Temp
        }
        else
        {
            Write-Output -InputObject $Bytes
        }
    }
}

Function Out-Hex {
    <#
        .SYNOPSIS
            Outputs a byte array to stdout in a hex representation.
 
        .DESCRIPTION
            This cmdlet takes a byte array and writes it to the console in line lengths that are easier to read. If the input array is sent through
            the pipeline, it does not need to be "protected" with a ',' to stop it from being unrolled.
 
        .PARAMETER InputObject
            The byte array to write to the console in hex.
 
        .PARAMETER Length
            The maximum number of bytes written to each line. This defaults to 8.
 
        .PARAMETER Delimiter
            The character used to separate each byte representation. This defaults to ' '.
 
        .EXAMPLE
            $Arr = @(0x01, 0x02, 0x03, 0x04)
            $Arr | Out-Hex -Length 2
 
            This writes out:
            01 02
            03 04
 
            In this case, the array $Arr is unrolled as it is sent to Out-Hex, so it ends up processing 4 separate Length 1 byte arrays.
 
        .EXAMPLE
            $Arr = @(0x01, 0x02, 0x03, 0x04, 0x05)
            $Arr | Out-Hex -Length 3
 
            This writes out:
            01 02 03
            04 05
 
            In this case, the array $Arr is not unrolled as it is sent to Out-Hex, so it ends up processing 1 array of length 5.
 
        .INPUTS
            System.Byte[]
 
        .OUTPUTS
            None
 
        .NOTES
            AUTHOR: Michael Haken
            LAST UPDATE: 1/19/2018
    #>

    [CmdletBinding()]
    [OutputType()]
    Param(
        [Parameter(ValueFromPipeline = $true, Position = 0)]
        [ValidateNotNull()]
        [System.Byte[]]$InputObject,

        [Parameter()]
        [ValidateRange(1, [System.Int32]::MaxValue)]
        [System.Int32]$Length = 8,

        [Parameter()]
        [ValidateNotNull()]
        [System.Char]$Delimiter = ' '
    )

    Begin {
        # This method will handle an array passed via pipeline whether it is unrolled or not
        # Keep track on how many items we've added to a single line so we know when to add a
        # new line
        [System.Int32]$LineCounter = 0
        [System.Text.StringBuilder]$SB = New-Object -TypeName System.Text.StringBuilder
    }

    Process {

        for ($i = 0; $i -lt $InputObject.Length; $i++)
        {
            $SB.Append($InputObject[$i + $j].ToString("X2") + $Delimiter) | Out-Null
            $LineCounter++

            # This pipeline input has finished the line, reset the line counter
            # and add the new line
            if ($LineCounter -eq $Length)
            {
                $LineCounter = 0

                # Trim the extra delimiter
                $SB.Length = $SB.Length - 1

                # Add the new line
                $SB.Append("`r`n") | Out-Null
            }            
        }
    }

    End {
        # Trim off the extra delimiters or new lines if they were added
        Write-Host ($SB.ToString().Trim())
    }
}

Function ConvertTo-OIDString {
    <#
        .SYNOPSIS
            Converts a byte array into an OID string.
 
        .DESCRIPTION
            This cmdlet accepts a byte array which is converted into an OID string.
 
        .PARAMETER InputObject
            The byte array to convert.
 
        .EXAMPLE
            $Arr = @(0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01)
            $OID = ConvertTo-OIDString $Arr
 
            This produces 1.2.840.113549.1.1.1 as the OID string, which is the OID for RSA Encryption
 
        .INPUTS
            System.Byte[]
 
        .OUTPUTS
            System.String
 
        .NOTES
            AUTHOR: Michael Haken
            LAST UPDATE: 1/20/2018
    #>
    
    [CmdletBinding()]
    [OutputType([System.String])]
    Param(
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
        [System.Byte[]]$InputObject
    )

    Begin {
        # Make a new array to hold all of the input in case the array is unrolled by the pipeline
        [System.Byte[]]$Bytes = @()
    }

    Process {
        # Add each input to the collector in case the byte array is unrolled on the pipeline
        $Bytes += $InputObject
    }

    End {
        if ($Bytes.Length -lt 3)
        {
            Write-Error -Exception (New-Object -TypeName System.ArgumentException("The input byte array must have a length of at least 3 bytes.")) -ErrorAction Stop
        }

        # Now that we have all the bytes to make the OID string, start processing them
        $Data = ""

        for ($i = 0; $i -lt $Bytes.Length; $i++)
        {
            if ($i -eq 0)
            {
                # Oid A.B.6.1....
                # Where A = 1 and B = 3
                # The first byte is computed by (A x 40) + B, first node times 40 plus the second node
                # To work backwords, do a modulo 40 to find the remainder, then subtract that from the value
                # to get a number divisible by 40 evenly to get the first node
                
                $Val = [System.Convert]::ToUInt32($Bytes[$i])
                [System.Int32]$SecondNode = $Val % 40
                [System.Int32]$FirstNode = ($Val - $SecondNode) / 40
                $Data += "$FirstNode.$SecondNode"
            }
            else
            {
                # All of the rest of the nodes are either less than or equal to 127, in which case they
                # contain the value directly, or they are 128 and greater and the value is stored in
                # multiple bytes
                if (($Bytes[$i] -band (1 -shl 7)) -ne 0)
                {
                    # Build new array to hold the bytes
                    [System.Byte[]]$Arr = @($Bytes[$i])

                    $OIDCounter = 1

                    while (($Bytes[$i + $OIDCounter] -band (1 -shl 7)) -ne 0)
                    {
                        # We don't care about the left most bit, it's just a marker
                        # that it is part of this node
                        $Arr += ($Bytes[$i + $OIDCounter] -band 0x7F)
                        $OIDCounter++
                    }

                    # This byte did not have a leading 1 and is the last byte
                    # to make up the variable length
                    $Arr += ($Bytes[$i + $OIDCounter] -band 0x7F)

                    # Skip however many bytes beyond the first we added to the array
                    $i += $Arr.Length - 1

                    # Take 7 bits from each byte and concatenate them

                    # This is the easiest way to make sure we grab 7 bits from each byte and concatenate
                    # them and is just as fast as bit shifting the individual bytes
                                       
                    [System.Text.StringBuilder]$SB = New-Object -TypeName System.Text.StringBuilder

                    for ($j = 0; $j -lt $Arr.Length; $j++)
                    {
                        # Convert the byte to base 2, then pad the left with 0's to make sure
                        # each string is 8 digits long
                        $Str = [System.Convert]::ToString($Arr[$j], 2).PadLeft(8, '0')

                        # Drop off the most significant bit and append to the string builder
                        $SB.Append($Str.Substring(1)) | Out-Null
                    }

                    # Convert our concatenated binary string to a UInt64 and tell the converter
                    # we're coming from base 2
                    [System.UInt64]$Value = [System.Convert]::ToUInt64($SB.ToString(), 2)
                                        
                    # This approach also works, but is more prone to problems in case the Multiplier
                    # exceeds 8 bytes
                    <#
                    $Multiplier = 1
 
                    for ($j = $Arr.Length - 1; $j -gt 0; $j--)
                    {
                        # Move the bits from the left byte to right byte,
                        # as we move farther down, we need to move more bytes
                        $Arr[$j] = ($Arr[$j - 1] -shl (8 - $Multiplier)) -bor $Arr[$j]
 
                        # Shift down the left byte
                        $Arr[$j - 1] = $Arr[$j - 1] -shr (1 * $Multiplier++)
                    }
 
                    $Arr = Set-ByteArrayPadding -InputObject $Arr -Length 8
 
                    if ([System.BitConverter]::IsLittleEndian)
                    {
                        [System.Array]::Reverse($Arr)
                    }
 
                    [System.UInt64]$Value = [System.BitConverter]::ToUInt64($Arr, 0)
                    #>


                    $Data += ".$Value"
                }
                else
                {
                    $Data += ".$([System.Convert]::ToUInt32($Bytes[$i]))"
                }
            }
        }

        Write-Output -InputObject $Data
    }
}

Function ConvertFrom-OIDString {
    <#
        .SYNOPSIS
            Creates a byte array from an OID string.
 
        .DESCRIPTION
            This cmdlet take an OID string in an X.Y.Z.W format and produces a byte array that is used
            to represent the OID.
 
        .PARAMETER OID
            The OID string to convert to bytes.
 
        .EXAMPLE
            $Bytes = ConvertFrom-OIDString -OID "1.2.840.113549.1.1.1"
 
            The contents of $Bytes is (in decimal): 42 134 72 134 247 13 1 1 1
 
            In hex the contents are: 2A 86 48 86 F7 0D 01 01 01
 
            This is the RSA Encryption OID.
 
        .INPUTS
            System.String
 
        .OUTPUTS
            System.Byte[]
 
        .NOTES
            AUTHOR: Michael Haken
            LAST UPDATE: 1/22/2018
    #>

    [CmdletBinding()]
    [OutputType([System.String])]
    Param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [System.String]$OID
    )

    Begin {

    }

    Process {
        # In case the OID components are seperated by spaces, replace those with
        # periods, then split the parts on the periods
        [System.String[]]$Parts = $OID.Trim().Replace(' ', '.').Split('.')

        if ($Parts.Length -gt 2)
        {
            # This will hold all of the resulting bytes
            [System.Byte[]]$Results = @()

            # Iterate each component of the OID string
            for ($i = 0; $i -lt $Parts.Length; $i++)
            {
                # The first two components require special handling, they
                # are composed from data in a single byte
                if ($i -eq 0)
                {
                    [System.UInt32]$High = $Parts[$i]
                    [System.UInt32]$Low = $Parts[$i + 1]

                    [System.UInt32]$FirstByte = ($High * 40) + $Low

                    [System.Byte[]]$Bytes = [System.BitConverter]::GetBytes($FirstByte)

                    if ([System.BitConverter]::IsLittleEndian)
                    {
                        [System.Array]::Reverse($Bytes)
                    }

                    # The GetBytes function will add 0x00 bytes to the result so that it is
                    # 4 bytes in length, but our result will only be 1 byte as part of the OID byte
                    # array
                    $Bytes = Remove-ByteArrayPadding -InputObject $Bytes

                    $Results += $Bytes

                    # Skip the second the part since we already used it
                    $i += 1
                }
                else
                {
                    # Get the byte integer value
                    [System.UInt32]$Value = $Parts[$i]

                    # If the value is less than 128, its value is just the byte
                    if ($Value -lt 128)
                    {
                        [System.Byte[]]$Bytes = [System.BitConverter]::GetBytes($Value)

                        if ([System.BitConverter]::IsLittleEndian)
                        {
                            [System.Array]::Reverse($Bytes)
                        }

                        # The GetBytes function will add 0x00 bytes to the result so that it is
                        # 4 bytes in length, but our result will only be 1 byte as part of the OID byte
                        # array
                        $Bytes = Remove-ByteArrayPadding -InputObject $Bytes

                        $Results += $Bytes
                    }
                    else
                    {
                        # The value is greater than 128, which means it uses multiple bytes to
                        # store the value.

                        [System.String]$BitString = [System.Convert]::ToString($Value, 2)
     
                        # Count how many individual bit characters we've added to a string
                        $Counter = 0

                        # Store all the bytes making up this value here
                        [System.Byte[]]$Bytes = @()

                        # Used as a buffer to store 7 digit bit strings
                        $Line = ""

                        # Create the bytes from the bit string, starting at the end of the string
                        for ($j = $BitString.Length - 1; $j -ge 0; $j += -1)
                        {
                            $Line = "$($BitString[$j])$Line"

                            $Counter++

                            # Once index 6 is set (meaning we've filled 7 digits), the counter is incremented to 7
                            # and then we need to reset it to start a new byte, or if this is the last digit
                            # make sure the line is 8 digits long
                            if ($Counter -ge 7 -or $j -eq 0)
                            {
                                # Since the string is 7 digits or less long, add 0's to pad
                                # to 8 digits
                                $Line = $Line.PadLeft(8, '0')
                                [System.Byte]$Byte = [System.Convert]::ToByte($Line, 2)
                            
                                $Bytes += $Byte
                                $Counter = 0
                                $Line = ""
                            }
                        }

                        # The += adds the items to the end, but we started at the back of the
                        # bit string, so we need to reverse the items
                        [System.Array]::Reverse($Bytes)

                        # Make all of the bytes except the last have a 1 in the most significant bit
                        for ($j = 0; $j -lt $Bytes.Length - 1; $j++)
                        {
                            $Bytes[$j] = $Bytes[$j] -bor 0x80
                        }

                        $Results += $Bytes
                    }
                }
            }

            Write-Output -InputObject $Results
        }
        else
        {
            Write-Error -Exception (New-Object -TypeName System.ArgumentException("The OID string was not correctly formatted, it should include at least 2 parts separated by a period.")) -ErrorAction Stop
        }
    }

    End {
    }
}