Skip to content

Instantly share code, notes, and snippets.

@AshleyPinner
Last active Aug 29, 2015
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@AshleyPinner AshleyPinner commented Aug 24, 2015

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

This comment has been minimized.

Copy link

@ircmaxell ircmaxell commented Aug 24, 2015

Can you reliably detect cygwin?

@AshleyPinner

This comment has been minimized.

Copy link
Owner Author

@AshleyPinner AshleyPinner commented Aug 24, 2015

@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