Skip to content

Instantly share code, notes, and snippets.

@AshleyPinner
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshleyPinner/f0b7cc8dc1d76585c56d to your computer and use it in GitHub Desktop.
Save AshleyPinner/f0b7cc8dc1d76585c56d to your computer and use it in GitHub Desktop.
The fun of cygwin and /dev/urandom (See comments, possibly MinGW related)
<?php
var_dump(is_readable('/dev/urandom'));
var_dump(passthru('head /dev/urandom'));
var_dump(PHP_OS);
?>
Sample output:
bool(false)
�����[
��-6<@������������@�L��_�����Ȏ��
NGצ�L���e��<�^=�f`?�bO\���yڽ=��n)p�:���Яب0p��|��h��K�Mr#��Iw���&�����##�xVHtԷ�m��
A��-�x����������E��yM~K8�ů( S��
cF���}O�
bH�ֹ�0��������;Ȍ'�u��8���ph*��nF�Z�FOX�Z��O8ڔt���%3Bz=RӨxl�U��5|� #�!j����A�S�>-~/�I��.(b~ r��Ð�/�榄�*�Ӑ ����^�Ý������moIք�X�-�(
K� �J��ji0�kT�,���^� u���p�+ 'ڰ��v�]Cw���%jI�I�^�Ң��&��g��-D0��փ`AE_EcQJ�C�~>��4Qh7#����A�ཹ �2g��/8/)=�;/�3W��P�H���*���*TB�Xm,w�*d��٨�������+��Фy� �� �6;�k��.9f�4� �V@`"��o���s��z�z�e���?)�(&�F��Iֲ��M�?���X��{! ���ږӝ{�F��T���SP�*+t� ��f2g^�
~�8�p���� �X4:�]Tċ�Ï�?ݜ��G��$��Sxh�!ku�a����Զ��W~����K�� ����;����D��i� N�݊�����������V�C��_���
0���q���?c[�|�'U��c�:�}՞
'.�{<�*���%Uثp��'��4Fs %��*�*�[4��v@���QWV�3�\��Z NtDn)k� �1�����P�
NULL
string(5) "WINNT"
@AshleyPinner
Copy link
Author

A terrible function to use this:

<?php


function cygwin_rand_bytes($bytes) {
    $buf = '';
    $remaining = (int)$bytes;
    do {
        $read = exec('head -c'.$remaining.' /dev/urandom', $throwaway, $return);
        if ($return !== 0) {
            $buf = false;
            break;
        }
        $remaining -= mb_strlen($read, '8bit');
        $buf .= $read;
    } while ($remaining > 0);


    if ($buf !== false) {
        if (mb_strlen($buf, '8bit') === $bytes) {
            return $buf;
        }
    }

    throw new Exception(
        'PHP failed to generate random data.'
    );

}

@ircmaxell
Copy link

Can you reliably detect cygwin?

@AshleyPinner
Copy link
Author

@ircmaxell This seems to be an artefact of MinGW. Cygwin might be detectable in various ENV or in the uname, but MinGW is very silent at the existence.

Either way, these both stem into the Windows Cryptographic API, so calling that would be preferable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment