Code Duplication    Length = 22-22 lines in 6 locations

src/library/sodium_compat/src/Compat.php 6 locations

@@ 578-599 (lines=22) @@
575
     * @throws Error
576
     * @throws TypeError
577
     */
578
    public static function crypto_auth($message, $key)
579
    {
580
        /* Type checks: */
581
        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
582
        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
583
584
        /* Input validation: */
585
        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) {
586
            throw new Error('Argument 2 must be CRYPTO_AUTH_KEYBYTES long.');
587
        }
588
589
        if (self::isPhp72OrGreater()) {
590
            return sodium_crypto_auth($message, $key);
591
        }
592
        if (self::use_fallback('crypto_auth')) {
593
            return call_user_func('\\Sodium\\crypto_auth', $message, $key);
594
        }
595
        if (PHP_INT_SIZE === 4) {
596
            return ParagonIE_Sodium_Crypto32::auth($message, $key);
597
        }
598
        return ParagonIE_Sodium_Crypto::auth($message, $key);
599
    }
600
601
    /**
602
     * Verify the MAC of a message previously authenticated with crypto_auth.
@@ 698-719 (lines=22) @@
695
     * @throws Error
696
     * @throws TypeError
697
     */
698
    public static function crypto_box_seal($plaintext, $publicKey)
699
    {
700
        /* Type checks: */
701
        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
702
        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
703
704
        /* Input validation: */
705
        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
706
            throw new Error('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
707
        }
708
709
        if (self::isPhp72OrGreater()) {
710
            return sodium_crypto_box_seal($plaintext, $publicKey);
711
        }
712
        if (self::use_fallback('crypto_box_seal')) {
713
            return call_user_func('\\Sodium\\crypto_box_seal', $plaintext, $publicKey);
714
        }
715
        if (PHP_INT_SIZE === 4) {
716
            return ParagonIE_Sodium_Crypto32::box_seal($plaintext, $publicKey);
717
        }
718
        return ParagonIE_Sodium_Crypto::box_seal($plaintext, $publicKey);
719
    }
720
721
    /**
722
     * Opens a message encrypted with crypto_box_seal(). Requires
@@ 733-754 (lines=22) @@
730
     * @throws Error
731
     * @throws TypeError
732
     */
733
    public static function crypto_box_seal_open($ciphertext, $keypair)
734
    {
735
        /* Type checks: */
736
        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
737
        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2);
738
739
        /* Input validation: */
740
        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
741
            throw new Error('Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.');
742
        }
743
744
        if (self::isPhp72OrGreater()) {
745
            return sodium_crypto_box_seal_open($ciphertext, $keypair);
746
        }
747
        if (self::use_fallback('crypto_box_seal_open')) {
748
            return call_user_func('\\Sodium\\crypto_box_seal_open', $ciphertext, $keypair);
749
        }
750
        if (PHP_INT_SIZE === 4) {
751
            return ParagonIE_Sodium_Crypto32::box_seal_open($ciphertext, $keypair);
752
        }
753
        return ParagonIE_Sodium_Crypto::box_seal_open($ciphertext, $keypair);
754
    }
755
756
    /**
757
     * Generate a new random X25519 keypair.
@@ 1577-1598 (lines=22) @@
1574
     * @throws Error
1575
     * @throws TypeError
1576
     */
1577
    public static function crypto_shorthash($message, $key)
1578
    {
1579
        /* Type checks: */
1580
        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
1581
        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
1582
1583
        /* Input validation: */
1584
        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SHORTHASH_KEYBYTES) {
1585
            throw new Error('Argument 2 must be CRYPTO_SHORTHASH_KEYBYTES long.');
1586
        }
1587
1588
        if (self::isPhp72OrGreater()) {
1589
            return sodium_crypto_shorthash($message, $key);
1590
        }
1591
        if (self::use_fallback('crypto_shorthash')) {
1592
            return call_user_func('\\Sodium\\crypto_shorthash', $message, $key);
1593
        }
1594
        if (PHP_INT_SIZE === 4) {
1595
            return ParagonIE_Sodium_Core32_SipHash::sipHash24($message, $key);
1596
        }
1597
        return ParagonIE_Sodium_Core_SipHash::sipHash24($message, $key);
1598
    }
1599
1600
    /**
1601
     * Expand a key and nonce into a keystream of pseudorandom bytes.
@@ 1698-1719 (lines=22) @@
1695
     * @throws Error
1696
     * @throws TypeError
1697
     */
1698
    public static function crypto_sign($message, $secretKey)
1699
    {
1700
        /* Type checks: */
1701
        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
1702
        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
1703
1704
        /* Input validation: */
1705
        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
1706
            throw new Error('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
1707
        }
1708
1709
        if (self::isPhp72OrGreater()) {
1710
            return sodium_crypto_sign($message, $secretKey);
1711
        }
1712
        if (self::use_fallback('crypto_sign')) {
1713
            return call_user_func('\\Sodium\\crypto_sign', $message, $secretKey);
1714
        }
1715
        if (PHP_INT_SIZE === 4) {
1716
            return ParagonIE_Sodium_Crypto32::sign($message, $secretKey);
1717
        }
1718
        return ParagonIE_Sodium_Crypto::sign($message, $secretKey);
1719
    }
1720
1721
    /**
1722
     * Validates a signed message then returns the message.
@@ 1905-1926 (lines=22) @@
1902
     * @throws Error
1903
     * @throws TypeError
1904
     */
1905
    public static function crypto_sign_detached($message, $secretKey)
1906
    {
1907
        /* Type checks: */
1908
        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
1909
        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
1910
1911
        /* Input validation: */
1912
        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
1913
            throw new Error('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
1914
        }
1915
1916
        if (self::isPhp72OrGreater()) {
1917
            return sodium_crypto_sign_detached($message, $secretKey);
1918
        }
1919
        if (self::use_fallback('crypto_sign_detached')) {
1920
            return call_user_func('\\Sodium\\crypto_sign_detached', $message, $secretKey);
1921
        }
1922
        if (PHP_INT_SIZE === 4) {
1923
            return ParagonIE_Sodium_Crypto32::sign_detached($message, $secretKey);
1924
        }
1925
        return ParagonIE_Sodium_Crypto::sign_detached($message, $secretKey);
1926
    }
1927
1928
    /**
1929
     * Verify the Ed25519 signature of a message.