How to hash some string with sha256 in Java ? Does anybody know any free library for this ?
I'm building an automated process to produce extensions. Is there a code example of calculating the extension-ID directly and entirely bypassing interaction with the browser?
(I'm answering my own question, below.)
I once unpacked the Clash Royale APK to see its contents. Noteworthy was a file called "Fingerprint.JSON", which contains paths to sound files and an associated hash value. My question is, why the hash value can be found in this file. I can imagine that before starting the app a hash value of an audio file is created and compared with the hash value in the file "Fingerprint.JSON". But that would not make sense, because I could also exchange the hash value itself to implement my own sounds.
Maybe someone can explain why these hash values can be stored publicly without causing security risks?
I am looking to decrypt a string using AES, I tried multiple solution from SO but none of them helped and I was unable to get the solution. The android developer used Cipher to do it below is the code for that:-
private static final byte[] initVector = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; private static Cipher getAES_Cipher(int opmode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { IvParameterSpec iv = new IvParameterSpec(initVector); SecretKeySpec skeySpec = new SecretKeySpec(Arrays.copyOfRange(getSHA(key), 0, 32), "AES"); Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5PADDING"); cipher.init(opmode, skeySpec, iv); return cipher;}
Similarly I tried using CryptoSwift to do it and below is the code I used to do it:-
extension String {func aesEncrypt(key: String, iv: String) throws -> String { let data: Array<UInt8> = (self.data(using: .utf8)?.bytes)! var key: Array<UInt8> = (key.data(using: .utf8)?.bytes)! let iv: Array<UInt8> = (iv.data(using: .utf8)?.bytes)! do { let encrypted = try AES(key: key, blockMode: CFB(iv: iv), padding: .pkcs5).encrypt(data) let encryptedData = Data(encrypted) let decrypted = try AES(key: key, blockMode: CFB(iv: iv), padding: .pkcs5).decrypt(encrypted) let decryptedData = Data(decrypted) let str = String.init(data: decryptedData, encoding: .utf8) print(str ?? String()) return encryptedData.base64EncodedString() } catch { print(error) return "error" }}func aesDecrypt(key: String, iv: String) throws -> String { let data: Array<UInt8> = (Data(base64Encoded: self)?.bytes)! let key: Array<UInt8> = (key.data(using: .utf8)?.bytes)! let iv: Array<UInt8> = (iv.data(using: .utf8)?.bytes)! do { let decrypted = try AES(key: key.sha256(), blockMode: CFB(iv: iv), padding: .pkcs5).decrypt(data) let decryptedData = Data(decrypted) guard let value = String.init(data: decryptedData, encoding: .utf8) else { return "error" } return value } catch { print(error) return "error" }}
}
and In ViewDidLoad() I called it like this:-
let message = "My Encrypted String From The Server" let test = try! message.aesDecrypt(key: "dfksjghlskjdfhglksjdfhglkjsdhfglkjhsfopweiurtypoweirutopiewrutgopiruegoijnsdeghsedrghesrerthigoererhehewthgewrhywertyweyweyewrtewrtyewihgoie", iv: "0000000000000000") print(test)
One more thing I want to highlight when I pass the same iv as android which is "{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }" I am getting the error from the CryptoSwift library as invalidInitializationVector but when I pass iv: "0000000000000000" I am getting an error -->
guard let value = String.init(data: decryptedData, encoding: .utf8) else { return "error" }
I believe it is something related to the iv that I am passing there.
Guys any help would be greatly appreciated!!
Thank You
This question already has an answer here:
I have to write method like this:
boolean isHash(String str){ ...}
Method should return true if input string is md5
hash or sha1
hash or sha256
hash.
Is it possible to implement such method ?
Please note that by viewing our site you agree to our use of cookies (see Privacy for details). You will only see this message once.