Explanation of HashMap#hash(int) method

>>> is the logical right shift (no sign-extension) (JLS 15.19 Shift Operators), and ^ is the bitwise exclusive-or (JLS 15.22.1 Integer Bitwise Operators). As to why this is done, the documentation offers a hint: HashMap uses power-of-two length tables, and hashes keys by masking away the higher bits and taking only the lower bits of … Read more

Multidimensional associative arrays in Bash

You can’t do what you’re trying to do: bash arrays are one-dimensional $ declare -A PERSONS $ declare -A PERSON $ PERSON[“FNAME”]=’John’ $ PERSON[“LNAME”]=’Andrew’ $ declare -p PERSON declare -A PERSON='([FNAME]=”John” [LNAME]=”Andrew” )’ $ PERSONS[1]=([FNAME]=”John” [LNAME]=”Andrew” ) bash: PERSONS[1]: cannot assign list to array member You can fake multidimensionality by composing a suitable array index … Read more

Why `additionalProperties` is the way to represent Dictionary/Map in Swagger/OpenAPI 2.0

Chen, I think your answer is correct. Some further background that might be helpful: In JavaScript, which was the original context for JSON, an object is like a hash map of strings to values, where some values are data, others are functions. You can think of each name-value pair as a property. But JavaScript doesn’t … Read more

How do I compare two hashes?

You can compare hashes directly for equality: hash1 = {‘a’ => 1, ‘b’ => 2} hash2 = {‘a’ => 1, ‘b’ => 2} hash3 = {‘a’ => 1, ‘b’ => 2, ‘c’ => 3} hash1 == hash2 # => true hash1 == hash3 # => false hash1.to_a == hash2.to_a # => true hash1.to_a == hash3.to_a … Read more

Piping Text To An External Program Appends A Trailing Newline

tl;dr: When PowerShell pipes a string to an external program: It encodes it using the character encoding stored in the $OutputEncoding preference variable It invariably appends a trailing (platform-appropriate) newline. Therefore, the key is to avoid PowerShell’s pipeline in favor of the native shell’s, so as to prevent implicit addition of a trailing newline: If … Read more

How to hash a password with SHA-512 in Java?

you can use this for SHA-512 (Not a good choice for password hashing). import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public String get_SHA_512_SecurePassword(String passwordToHash, String salt){ String generatedPassword = null; try { MessageDigest md = MessageDigest.getInstance(“SHA-512”); md.update(salt.getBytes(StandardCharsets.UTF_8)); byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); for(int i=0; i< bytes.length ;i++){ sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, … Read more

Generate hash from UIImage

I wound up using the following code to accomplish the task. Note that this requires that you import <CommonCrypto/CommonDigest.h>: unsigned char result[CC_MD5_DIGEST_LENGTH]; NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(inImage)]; CC_MD5([imageData bytes], [imageData length], result); NSString *imageHash = [NSString stringWithFormat: @”%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X”, result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ];

MD5 hash from file in C++

Here’s a straight forward implementation of the md5sum command that computes and displays the MD5 of the file specified on the command-line. It needs to be linked against the OpenSSL library (gcc md5.c -o md5 -lssl) to work. It’s pure C, but you should be able to adapt it to your C++ application easily enough. … Read more