lundi 22 décembre 2014

Appending random bytes to an image doesn't change the outcome?

So to simplify the situation I want to append a couple random bytes to an image to change the MD5 hash every time.


I have the code set up to look up the image then create an NSImage. After that it converts the NSImage to NSMutableData which offers me the opportunity to append random bytes. I then end it all by exporting the altered NSImage to the desktop.


That all works fine and dandy until I run my program twice and compare the MD5 hashes of the two outputs. They are exactly the same! It doesn't matter if I append 1 or 1000 random bytes, if you compare the two outputs, it is exactly the same to each other.


My code:



- (void)createNewImage:(NSString *)filePath {
// NSImage from path
NSImage *newImage = [[NSImage alloc]initWithContentsOfFile:filePath];

// NSData to NSMutableData
NSData *imgData = [newImage TIFFRepresentation];
NSMutableData *mutableData = [[NSMutableData alloc]initWithData:imgData];

// Get the random bytes
NSData *randomData = [self createRandomBytes:10];

// Append random data to new image
[mutableData appendData:randomData];

(etc...)

// Cache the new image
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:mutableData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
NSData *newData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[newData writeToFile:fileName atomically:NO];

}

-(NSData *)createRandomBytes:(NSUInteger)amount {
return [[NSFileHandle fileHandleForReadingAtPath:@"/dev/random"] readDataOfLength:amount];
}




Aucun commentaire:

Enregistrer un commentaire