Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl flash.crypto.generateRandomBytes (google code Issue 103) #38

Closed
zwetan opened this issue Dec 28, 2015 · 2 comments
Closed

impl flash.crypto.generateRandomBytes (google code Issue 103) #38

zwetan opened this issue Dec 28, 2015 · 2 comments
Labels
AVMGlue The Flash Platform API enhancement
Milestone

Comments

@zwetan
Copy link
Member

zwetan commented Dec 28, 2015

https://code.google.com/p/redtamarin/issues/detail?id=103


see: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/crypto/package.html#generateRandomBytes()

signature:

public function generateRandomBytes(numberRandomBytes:uint):ByteArray

under Macintosh / Linux we can use /dev/urandom
$ cat /dev/urandom | LC_CTYPE=C tr -dc 'A-F0-9' | fold -w 2 | sed 1q

in AS3 that would gives

var str:String = Program.open( "cat /dev/urandom | LC_CTYPE=C tr -dc 'A-F0-9' | fold -w 2 | sed 1q" );
var byte:uint = uint( "0x" + str );

Under Windows we need to use CryptGenRandom()
see PHP implementation of php_win32_get_random_bytes
https://github.com/php/php-src/blob/master/win32/winutil.c#L80


because Program.open() call the CLI as sh -c ""

it is better to do like that

var str:String = Program.open( "LC_CTYPE=C tr -dc 'A-F0-9' < /dev/urandom | head -c 2" );

shorter command line, less dependencies and no risk to block the CLI and end up in an infinite loop while running the function, also avoid a carriage return at end of line


@zwetan
Copy link
Member Author

zwetan commented Dec 28, 2015

we need to test this under Windows with cygwin_compat

eg. do we need to call something like
C:/cygwin/bin/bash.exe -c 'LC_CTYPE=C tr -dc 'A-F0-9' < /dev/urandom | head -c 2'
or
C:/cygwin/bin/sh.exe -c 'LC_CTYPE=C tr -dc 'A-F0-9' < /dev/urandom | head -c 2'

@zwetan zwetan added this to the 0.4.2 milestone Jan 29, 2016
@zwetan zwetan mentioned this issue May 28, 2016
@zwetan zwetan added the AVMGlue The Flash Platform API label May 28, 2016
@zwetan zwetan modified the milestones: 0.4.3, 0.4.2 Jul 30, 2016
@zwetan zwetan modified the milestones: 0.4.3, 0.4.2 Apr 7, 2017
@zwetan
Copy link
Member Author

zwetan commented Apr 7, 2017

we added a new C.stdlib function rand_s()
inspired by MSDN rand_s

Under Windows it reuse CRT rand_s() which uses RtlGenRandom()
see RtlGenRandom

Under POSIX systems (Linux and macOS) we implemented rand_s()
reading 1 byte from "/dev/urandom"

for flash.crypto.generateRandomBytes()

under Windows, we call rand_s() in a loop to fill a ByteArray

Under POSIX systems (Linux and macOS) we apply the same logic as the rand_s() implementation
reading N amount of bytes from "/dev/urandom"

@zwetan zwetan closed this as completed Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AVMGlue The Flash Platform API enhancement
Projects
None yet
Development

No branches or pull requests

1 participant