Completed
Push — master ( 635b37...9d1f6a )
by Antonio Carlos
02:14
created

Base32::generateBase32RandomKey()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
1
<?php
2
3
namespace PragmaRX\Google2FA\Support;
4
5
use ParagonIE\ConstantTime\Base32 as ParagonieBase32;
6
7
trait Base32
8
{
9
    /**
10
     * Generate a digit secret key in base32 format.
11
     *
12
     * @param int $length
13
     *
14
     * @return string
15
     */
16
    public function generateBase32RandomKey($length = 16, $prefix = '')
17
    {
18
        $b32 = '234567QWERTYUIOPASDFGHJKLZXCVBNM';
19
20
        $secret = $prefix ? $this->toBase32($prefix) : '';
21
22
        for ($i = 0; $i < $length; $i++) {
23
            $secret .= $b32[$this->getRandomNumber()];
0 ignored issues
show
Bug introduced by
It seems like getRandomNumber() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24
        }
25
26
        $this->validateSecret($secret);
0 ignored issues
show
Bug introduced by
It seems like validateSecret() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
28
        return $secret;
29
    }
30
31
    /**
32
     * Decodes a base32 string into a binary string.
33
     *
34
     * @param string $b32
35
     *
36
     * @throws InvalidCharactersException
37
     *
38
     * @return int
39
     */
40
    public function base32Decode($b32)
41
    {
42
        $b32 = strtoupper($b32);
43
44
        $this->validateSecret($b32);
0 ignored issues
show
Bug introduced by
It seems like validateSecret() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
46
        return ParagonieBase32::decodeUpper($b32);
47
    }
48
49
    /**
50
     * Encode a string to Base32.
51
     *
52
     * @param $string
53
     *
54
     * @return mixed
55
     */
56
    public function toBase32($string)
57
    {
58
        $encoded = ParagonieBase32::encodeUpper($string);
59
60
        return str_replace('=', '', $encoded);
61
    }
62
}
63