sl@0: /*
sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: * Generates C code that initialises a variable to hold some random data.
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: /**
sl@0:  @file
sl@0: */
sl@0: 
sl@0: #include <openssl/rand.h>
sl@0: 
sl@0: static void gen_random(int len)
sl@0:     {
sl@0:     unsigned char data[len];
sl@0:     int i, j;
sl@0: 
sl@0:     RAND_bytes(data, len);
sl@0: 
sl@0:     printf("unsigned char random[] =");
sl@0:     for (i = 0 ; i < sizeof(data) ; i += 16)
sl@0:         {
sl@0:         printf("\n\t\"");
sl@0:         for (j = i ; j < sizeof(data) && j < (i + 16) ; ++j)
sl@0:             {
sl@0:             printf("\\x%02x", data[j]);
sl@0:             }
sl@0:         printf("\"");
sl@0:         }
sl@0: 
sl@0:     printf(";\n");
sl@0:     }
sl@0: 
sl@0: static void badUsage()
sl@0:     {
sl@0:     printf("gen_random [BYTES]\n");
sl@0:     exit(1);
sl@0:     }
sl@0: 
sl@0: int main(int argc, char* argv[])
sl@0:     {
sl@0:     int bytes = 256;
sl@0: 
sl@0:     if (argc > 2)
sl@0:         badUsage();
sl@0:     else if (argc == 2)
sl@0:         {
sl@0:         bytes = atoi(argv[1]);
sl@0:         if (bytes < 1)
sl@0:             badUsage();
sl@0:         }
sl@0: 
sl@0:     gen_random(bytes);
sl@0:     return 0;
sl@0:     }