Remove duplicated code for generate random bytes.