public static byte[] scrambleCachingSha2(byte[] password, byte[] seed) throws DigestException {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException ex) {
            throw new DigestException(ex);
        }

        byte[] dig1 = new byte[CACHING_SHA2_DIGEST_LENGTH];
        byte[] dig2 = new byte[CACHING_SHA2_DIGEST_LENGTH];
        byte[] scramble1 = new byte[CACHING_SHA2_DIGEST_LENGTH];

        // SHA2(src) => digest_stage1
        md.update(password, 0, password.length);
        md.digest(dig1, 0, CACHING_SHA2_DIGEST_LENGTH);
        md.reset();

        // SHA2(digest_stage1) => digest_stage2
        md.update(dig1, 0, dig1.length);
        md.digest(dig2, 0, CACHING_SHA2_DIGEST_LENGTH);
        md.reset();

        // SHA2(digest_stage2, m_rnd) => scramble_stage1
        md.update(dig2, 0, dig1.length);
        md.update(seed, 0, seed.length);
        md.digest(scramble1, 0, CACHING_SHA2_DIGEST_LENGTH);

        // XOR(digest_stage1, scramble_stage1) => scramble
        byte[] mysqlScrambleBuff = new byte[CACHING_SHA2_DIGEST_LENGTH];
        xorString(dig1, mysqlScrambleBuff, scramble1, CACHING_SHA2_DIGEST_LENGTH);

        return mysqlScrambleBuff;
    }
@Test
    public void testScrambleCachingSha2() throws DigestException {
        byte[] bytes1 = new byte[]{73, -38, 6, -106, 14, -28, -98, -32,
                -80, -49, -88, -66, -116, -101, -86, 25, -7, 32, 44, -118,
                24, -128, -8, 12, 10, -38, 111, -11, 42, -111, 43, -123};

        byte[] bytes2 = new byte[]{-86, 63, -63, 80, 93, 3, 105, -59, 71,
                -41, 81, 112, 35, -29, 28, -115, -68, 16, -119, -60, -53,
                -80, -4, -19, 60, -37, 27, -22, -23, -23, 49, -36};

        Assert.assertArrayEquals(bytes1, MySQLPasswordEncrypter
                .scrambleCachingSha2(new byte[0], new byte[0]));
        Assert.assertArrayEquals(bytes2, MySQLPasswordEncrypter
                .scrambleCachingSha2(
                        new byte[]{1, 2, 3, 4, 5, 6, 7, 8}, new byte[]{1, 1}));
    }