mercredi 28 janvier 2015

Java - Generating a random salt isn't random

I'm trying to generate a salt in Java to use with a hashing algorithm for secure password storage. I'm using the following code to create the random salt:



private static String getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
System.out.println(salt.toString());
return salt.toString();
}


Which should generate a completely secure, randomly generated salt to use in my hashing algorithm. When I run the code however, it keeps outputting the same salt every time... Indicating that the salt being generated isn't random at all.


For obvious security purposes, each user needs a unique salt however if I use this code each time a new account is created then every user will have the same salt, defeating the purpose of having it in the first place.


My question is this: Why does this keep giving me the same salt and what can I do to ensure the salt generated is completely random each time the code is run?





Android ArrayList without first value

I have problem with ArrayList. I have the question files structure like:


Y aaaaaa.


Y bbbbbb.


N ccccccc.


N ddddddd.


and I want to put into textview3 the first letter like Y or N



public void value (){

Scanner answerScanner = new Scanner(getResources().openRawResource(R.raw.answer));
Scanner questionScanner = new Scanner(getResources().openRawResource(R.raw.question));

ArrayList<String> answerList = new ArrayList<String>();
ArrayList<String> questionList = new ArrayList<String>();

try {
while (answerScanner.hasNextLine() ) {
answerList.add(answerScanner.nextLine());
questionList.add(questionScanner.nextLine());
}
} finally {
answerScanner.close();
questionScanner.close();
}

int nextInt = random.nextInt(questionList.size());

String answerString = answerList.get(nextInt);
String questionString = questionList.get(nextInt);

yourAnswerString = answerString.substring(0);
yourQuestionString = questionString.substring(2);
shortform = questionString.substring(0,1);


TextView textGenerateNumber = (TextView)findViewById(R.id.textView1);
TextView textGenerateNumber2 = (TextView)findViewById(R.id.textView2);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber.setText(yourQuestionString);
textGenerateNumber2.setText(yourAnswerString);
textGenerateNumber3.setText(shortform);
}


then I add:



shortform = questionString.substring(0,1);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber3.setText(shortform);


I get good answers I all cases without first .... when radom get first line from text file I get empty value ( my textView3 are empty).





Python:Trying to make a circle at a random spot, with a random radius, that is filled randomly with a set of given color. Stuck on color part

# Heres what I have so far. The colorCircle(myTurtle) doesn't seem to work and I don't really know what to do. Kinda of an amateur, any tips would be appreciated.



def drawPolygon(myTurtle,sideLength,numSides):
turnAngle = 360/numSides
for i in range(numSides):
myTurtle.forward(sideLength)
myTurtle.right(turnAngle)

def drawCircle(myTurtle,radius):
circumference = 2 * 3.1415 * radius
sideLength = circumference / 360
drawPolygon(myTurtle, sideLength, 360)

def colorCircle(myTurtle):
x = random.random()
y = random.random()
myTurtle.up()
myTurtle.goto(x,y)
myTurtle.down()
radius = random.random()
color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)]
random.randint(0,9)
myTurtle.begin_fill(color)
drawCircle(myTurtle)




mardi 27 janvier 2015

Mysql join tables Randomly

I have two tables. (say TableA and TableB)

TableA have 10 rows and TableB have 50 rows.

I want each row in TableA should be joined with X number of rows from TableB randomly, so that i get 500 unique results


For example if TableA has rows (a,b,c) and TableB have rows (p,q,r,s,t,u) and X=2 ,then expected result is:



a-p
a-q
b-r
b-s
c-t
c-u


This joining should be random





Improve Powershell Performance to Generate a Random File

I'd like to user Powershell to create a random text file for use in basic system testing (upload, download, checksum, etc). I've used the following articles and come up with my own code snippet to create a random text file but the performance is terrible.



Here is my code sample that takes approximately 227 seconds to generate a 1MB random text file on a modern Windows 7 Dell laptop. Run time was determined using the Measure-Command cmdlet. I repeated the test several times during different system load with similar long runtime results.



# select characters from 0-9, A-Z, and a-z
$chars = [char[]] ([char]'0'..[char]'9' + [char]'A'..[char]'Z' + [char]'a'..[char]'z')
# write file using 128 byte lines each with 126 random characters
1..(1mb/128) | %{-join (1..126 | %{get-random -InputObject $chars }) } `
| out-file test.txt -Encoding ASCII


I am looking for answers that discuss why this code has poor performance and suggestions for simple changes I can make to improve the runtime for generating a similar random text file (ASCII text lines of 126 random alphanumeric characters - 128 bytes with "\r\n" EOL, output file an even number of megabytes such as the above 1MB sample). I would like file output to be written in pieces (one or more lines at a time) so that we never need a string the size of the output file stored in memory.





random function with a same probability for each value in R

is there any built in function in R, which I could use to generate a random value between the value I choose for with the same probability for example I have a vector which I want to assign 1 or 0 with 50% probability to each value. is there any function which I could say what are my values and according to that the probabilities in other words: values(A,B,C,F), with a 0.25 probability for each and then assign that random value to each vector value. thanks in advance,





How to generate random levels for a balance game

I want to make a game like this one: http://ift.tt/1CwhmId


And I'm having troubles when trying to create the levels randomly, with an increasing difficulty.


The game consist in 1 or more balances and some objects that are placed on each side of them so that you have to determine which is the heaviest object.


There could be 1 to 4 simultaneous balances and a maximum of 2 objects per side on every balance. The amount of objects increases accordingly to the difficulty of the current level.


What i would like you to help me with is on the generation of the levels. I would like to know a good way to choose the amount of balances, the amount of objects, the weight of the objects, the amount of objects per side on the balance, and having in mind that it may happen that not every configuration leads to a 100% determinable result.


Per example, if I choose a balance and put 2 objects on each side and the sum of the weight of the objects on each side sums equal weight, the balance will not move and stay still, and it won't be possible for the user to determine which object is the heaviest.


How can I approach this problem? I'll appreciate any help that you can give me.


Thank you in advance!


Regards.