| @@ 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. |
|
| @@ 551-570 (lines=20) @@ | ||
| 548 | * |
|
| 549 | * @return float|int |
|
| 550 | */ |
|
| 551 | protected function calculate_entropy_bits( $password ) { |
|
| 552 | $bits = 0; |
|
| 553 | // Calculate the score. |
|
| 554 | $charset_score = log( $this->get_charset_size( $password ) ) / log( 2 ); |
|
| 555 | ||
| 556 | $aidx = $this->get_char_index( $password[0] ); |
|
| 557 | $length = strlen( $password ); |
|
| 558 | ||
| 559 | for ( $b = 1; $b < $length; $b ++ ) { |
|
| 560 | $bidx = $this->get_char_index( $password[ $b ] ); |
|
| 561 | ||
| 562 | // 27 = number of chars in the index (a-z,' '). |
|
| 563 | $c = 1.0 - $this->frequency_table[ $aidx * 27 + $bidx ]; |
|
| 564 | ||
| 565 | // Increment the bits. |
|
| 566 | $bits += $charset_score * $c * $c; |
|
| 567 | ||
| 568 | // Move on to next pair. |
|
| 569 | $aidx = $bidx; |
|
| 570 | } |
|
| 571 | ||
| 572 | return $bits; |
|
| 573 | } |
|