| @@ 530-549 (lines=20) @@ | ||
| 527 | * |
|
| 528 | * @param String $password the password. |
|
| 529 | */ |
|
| 530 | protected function calculate_entropy_bits( $password ) { |
|
| 531 | $bits = 0; |
|
| 532 | $charset_score = log( $this->get_charset_size( $password ) ) / log( 2 ); |
|
| 533 | ||
| 534 | $aidx = $this->get_char_index( $password[0] ); |
|
| 535 | $length = strlen( $password ); |
|
| 536 | ||
| 537 | for ( $b = 1; $b < $length; $b++ ) { |
|
| 538 | $bidx = $this->get_char_index( $password[ $b ] ); |
|
| 539 | ||
| 540 | // 27 = number of chars in the index (a-z,' '). |
|
| 541 | $c = 1.0 - $this->frequency_table[ $aidx * 27 + $bidx ]; |
|
| 542 | $bits += $charset_score * $c * $c; |
|
| 543 | ||
| 544 | // Move on to next pair. |
|
| 545 | $aidx = $bidx; |
|
| 546 | } |
|
| 547 | ||
| 548 | return $bits; |
|
| 549 | } |
|
| 550 | ||
| 551 | /** |
|
| 552 | * A frequency table of character pairs, starting with ' ' then ' a', ' b' [...] , 'a ', 'aa' etc. |
|
| @@ 558-577 (lines=20) @@ | ||
| 555 | * |
|
| 556 | * @return float|int |
|
| 557 | */ |
|
| 558 | protected function calculate_entropy_bits( $password ) { |
|
| 559 | $bits = 0; |
|
| 560 | // Calculate the score. |
|
| 561 | $charset_score = log( $this->get_charset_size( $password ) ) / log( 2 ); |
|
| 562 | ||
| 563 | $aidx = $this->get_char_index( $password[0] ); |
|
| 564 | $length = strlen( $password ); |
|
| 565 | ||
| 566 | for ( $b = 1; $b < $length; $b ++ ) { |
|
| 567 | $bidx = $this->get_char_index( $password[ $b ] ); |
|
| 568 | ||
| 569 | // 27 = number of chars in the index (a-z,' '). |
|
| 570 | $c = 1.0 - $this->frequency_table[ $aidx * 27 + $bidx ]; |
|
| 571 | ||
| 572 | // Increment the bits. |
|
| 573 | $bits += $charset_score * $c * $c; |
|
| 574 | ||
| 575 | // Move on to next pair. |
|
| 576 | $aidx = $bidx; |
|
| 577 | } |
|
| 578 | ||
| 579 | return $bits; |
|
| 580 | } |
|