Esempio n. 1
0
ISerialize* convertJsonToBinJson(const char* data)
{
    rapidjsonGenericDocument doc;
    if (doc.Parse<0>(data).HasParseError())
    {
        return nullptr;
    }
    if (doc.IsArray())
    {
        std::vector<ISerialize*> vector;
        vector.reserve(doc.Size());
        for (rapidjson::SizeType size = doc.Size(), i = 0; i < size; i++)
        {
            vector.push_back(convertBin(doc[i]));
        }
        return new SerializeVector(vector);
    }
    if (doc.IsObject())
    {
        std::map<std::string, ISerialize*> map;
        for (rapidjsonValue::ConstMemberIterator itr = doc.MemberBegin(), end = doc.MemberEnd(); itr != end; ++itr)
        {
            map.insert(std::make_pair(itr->name.GetString(), convertBin(itr->value)));
        }
        return new SerializeMap(map);
    }
    return nullptr;
}
Esempio n. 2
0
int main()
{
    word16 x=1111; // nombre entre 1000 et 9999 pour Von Neumann
    struct mt19937p mt; // Pour Mersenne-Twister
    int tmp = rand(); // Pour Mersenne-Twister
    u32 Kx[NK], Kex[NB*NR], Px[NB]; // pour l'AES

    int n = 1024; // Echantillon de test

    int * output_rand = malloc(sizeof(int)*n); // sortie du rand du C
    int ** output_rand_fort = malloc(sizeof(sizeof(int)*4)*n); // sortie du rand du C	(4 bits poids fort)
    int ** output_rand_faible = malloc(sizeof(sizeof(int)*4)*n); // sortie du rand du C (4 bits poids faible)

    word32 * output_AES = malloc(sizeof(word32)*n); // sortie pour l'AES
    word16 * output_VN = malloc(sizeof(word16)*n); // sortie pour pour Von Neumann
    word32 * output_MT = malloc(sizeof(word32)*n); // sortie pour Mersenne-Twister


    // initialisation des graines des generateurs
    srand(rdtsc());  // rand du C
    sgenrand(time(NULL)+(tmp), &mt); // Mersenne-Twister
    // Initialisation de la clé et du plaintext pour l'AES
    // 45 est un paramètre qui doit changer à chaque initialisation
    init_rand(Kx, Px, NK, NB, 45);
    KeyExpansion(Kex,Kx); // AES : sous-clefs


    for(int i = 0; i < n; i++)
    {
        // sorties des generateurs
        output_rand[i] = rand(); // rand du C
        int* tempBin = convertBin(output_rand[i], taille_int);
        output_rand_fort[i] = extractBit(tempBin, 1, taille_int);
        output_rand_faible[i] = extractBit(tempBin, 0, taille_int);

        for(int j = 0; j < 4; j++)
        {
            if(!(tempBin[taille_int-1-j] == output_rand_faible[3-j]))
            {
                printf("PAS OK !!!!");
            }
        }


        output_VN[i] = Von_Neumann(&x); // Von Neumann
        output_MT[i] = genrand(&mt); // Mersenne-Twister
        output_AES[i] = AES(Px, Kex); // AES
    }

    // affichage

    /*printf("- Generation de nombres aleatoires -\n");
    printf("rand du C (poids fort) :\n");
    for(int i = 0; i < n; i++)
    {
    	checkBin(output_rand_fort[i], 4);
    }
    /*printf("rand du C (poids faible) :\n");
    for(int i = 0; i < n; i++)
    {
    	displayBinToDec(output_rand_faible[i],4);
    }
    printf("Von Neumann : \n");
    displayValuesWord16(output_VN, n);
    printf("Mersenne Twister : \n");
    displayValuesWord32(output_MT, n);
    printf("AES : \n");
    displayValuesWord32(output_AES, n);
    */
    // tests
    double testFreq_randFort = Frequency(output_rand_fort, n, 4);
    //double testFreq_randFaible = Frequency(output_rand_faible, n, 4);
    /*double testFreq_aes = Frequency(makeArrayWord32(output_AES,n), n, 32);
    double testFreq_VN = Frequency(makeArrayWord16(output_VN,n), n, 16);
    double testFreq_MT = Frequency(makeArrayWord32(output_MT,n), n, 32);

    printf("PValeurs : \n \
    Rand Bits Poids Fort : %lf\n \
    Rand Bits Poids Faible : %lf\n \
    AES : %lf\n \
    VN : %lf\n \
    MT : %lf\n",testFreq_randFort,testFreq_randFaible,testFreq_aes,testFreq_VN,testFreq_MT);*/
    return 0;
}
Esempio n. 3
0
ISerialize* convertBin(const rapidjsonValue& json)
{

    if (json.IsObject())
    {
        std::map<std::string, ISerialize*> map;
        for (rapidjsonValue::ConstMemberIterator itr = json.MemberBegin(), end = json.MemberEnd(); itr != end; ++itr)
        {
            map.insert(std::make_pair(itr->name.GetString(), convertBin(itr->value)));
        }
        return new SerializeMap(std::move(map));
    }

    if (json.IsArray())
    {
        std::vector<ISerialize*> vector;
        vector.reserve(json.Size());
        for (::rapidjson::SizeType i = 0, size = json.Size(); i < size; i++)
        {
            vector.push_back(convertBin(json[i]));
        }
        return new SerializeVector(std::move(vector));
    }
    if (json.IsNull())
    {
        return new SerializeNull();
    }
    if (json.IsBool())
    {
        return new SerializeBool(json.GetBool());
    }
    if (json.IsInt())
    { //https://msdn.microsoft.com/en-us/library/7fh3a000.aspx
        int32_t value = json.GetInt();

        if (value >= std::numeric_limits<int8_t>::min() && value <= std::numeric_limits<int8_t>::max())
        {
            return new SerializeInt8(value);
        }
        if (value >= std::numeric_limits<int16_t>::min() && value <= std::numeric_limits<int16_t>::max())
        {
            return new SerializeInt16(value);
        }

        return new SerializeInt32(std::move(value));
    }
    if (json.IsUint())
    {
        uint32_t value = json.GetUint();
        if (value <= std::numeric_limits<uint8_t>::max())
        {
            return new SerializeInt8(value);
        }
        if (value <= std::numeric_limits<uint16_t>::max())
        {
            return new SerializeInt16(value);
        }

        return new SerializeUInt32(std::move(value));
    }
    if (json.IsInt64())
    {
        return new SerializeInt64(json.GetInt64());
    }
    if (json.IsUint64())
    {
        return new SerializeUInt64(json.GetInt64());
    }
    if (json.IsString())
    {
        return new SerializeString(json.GetString());
    }
    if (json.IsDouble())
    {
        double d = json.GetDouble();
        float p = (float)d;
        if (d == (double)p)
        {
            return new SerializeFloat(std::move(p));
        }
        return new SerializeDouble(std::move(d));
    }
    return nullptr;
}