I have to perform the following encoding on a string:
Calculate SHA1 hash value, with output in hexadecimal format
Apply a BASE64 encoding to the resulted hash
I'm trying to do this in the code below, 'str' being the input and 'digest' the final encoded string.
I've tried different ways without much success. The current code (below) throws an error:
WrappedException: Wrapped java.lang.IllegalArgumentException: contains illegal character for hexBinary: crypto.SecretKey
var str = nonce + timestamp + secret;
var secureString = HTTPS.createSecureString({
input: str
});
secureString = secureString.hash({
algorithm: CRYPTO.HashAlg.SHA1
});
var hexString = HTTPS.createSecretKey({
encoding: HTTPS.Encoding.HEX,
guid: secureString
});
var digest = ENCODE.convert({
string: hexString,
inputEncoding: ENCODE.Encoding.HEX,
outputEncoding: ENCODE.Encoding.BASE_64
});
Related
am trying to make a signature for a HTTP request ,
using flutter/dart for the app and the server in NodeJs
but i have a problem there is little different between the two signature
any idea what cause that
EEFSxb_coHvGM-69RhmfAlXJ9J0= //signature in dart
EEFSxb/coHvGM+69RhmfAlXJ9J0= //signature in nodejs
signature.dart
var key = "key";
var data = "data";
List<int> signingKey = utf8.encode("key");
List<int> signatureBaseString = utf8.encode("data");
var hmacSha1 = Hmac(sha1, signingKey);
var digest = hmacSha1.convert(signatureBaseString);
var hashInBase = base64Url.encode(digest.bytes);
print(hashInBase) ; // result : EEFSxb_coHvGM-69RhmfAlXJ9J0=
signature.js
var data = "data" ;
var key = "key" ;
var output = encodeURIComponent(data);
var keyO = encodeURIComponent(key);
var hashed = CryptoJS.HmacSHA1(output , keyO);
var hashInBase = CryptoJS.enc.Base64.stringify(hashed);
console.log(hashInBase); // result : EEFSxb/coHvGM+69RhmfAlXJ9J0=
There are two styles of Base64 encoding which use slightly different character sets for the 64 characters. (You get 52 from the upper and lower case letters and ten more from the numbers, so a couple more ascii chars are needed to make up the 64 - plus the special trailing equals.)
The / and + characters have special meanings in URLs, so can be replaced in the alternative "URL safe" encoding with _ and -. Note also that the trailing equals can be dropped.
It seems you may be trying to compare base64 encoded strings to test for equality. It's safer to decode from base 64 and compare the byte arrays.
Anyway, to solve you problem, don't use the Dart URL safe version, use the regular version: base64.encode()
I have an base64 string which has unicode such as \u,\n and so on. while trying to decode base64 string,, I am getting FormatException: Invalid character (at character 308135) pointing to \u.
This is how my code look like:
var formTemplate = "asdaskdsd788\uhvsajdasd\u==sdsd..."; //base64 string
try {
var base64decode = utf8.decode(base64.decode(formTemplate));
} catch (e) {
print(e);
}
I end up using strig_escape plugin. Any other solution are welcome.
eg.
var base64String = "21dsdsadas\udsads=\udsd";
var decodeString = unescape(base64String);
I'm passing password into sha256. I successfully create sha256 and can also print it. The problem begins when I'm trying to convert digest.bytes into a string and append it.
import 'package:crypto/crypto.dart';
var url = "http://example_api.php?";
url += '&hash=';
// hash the password
var bytes = utf8.encode(password);
var digest = sha256.convert(bytes);
print("Digest as hex string: $digest");
url += String.fromCharCodes(digest.bytes);
This is printed: Digest as hex string: 03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4
This is appended to url: ¬gBóá\vá¥âUðg6#ȳ´Eùx×ÈFô
What am I doing wrong? I also tried utf8.decode method but using it gives me an error.
When you print digest, the print method will call digest.toString(), which is implemented to return a string of the digest bytes using a hexadecimal representation. If you want the same thing you have several options:
Call digest.toString() explicitly (or implicitly)
final digestHex = digest.toString(); // explicitly
final digestHex = '$digest'; // implicitly
Map the byte array to its hexadecimal equivalent
final digestHex = digest.bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
Use the convert package (this is what the crypto package does)
import 'package:convert/convert.dart';
...
final digestHex = hex.encode(digest.bytes);
The reason you are getting an error using utf8.decode is that your digest isn't an encoded UTF-8 string but a list of bytes that for all intents and purposes are completely random. You are trying to directly convert the bytes into a string, and doing so is easier if you can assume that they already represent a valid string. With the byte output from a hashing algorithm, though, you cannot safely make such an assumption.
However, if for some reason you still want to use this option, use the second optional parameter for utf8.decode to force it to try and decode the bytes anyway:
final digestString = utf8.decode(bytes, allowMalformed: true);
For reference, a byte list of [1, 255, 47, 143, 6, 80, 33, 202] results in "�/�P!�" where "�" represents an invalid/control character. You do not want to use this option, especially where the string will become part of a URL (as it's virtually guaranteed that the resulting string will not be web-safe).
For the hexadecimal representation of a Digest object, please explicitly call Digest.toString() (though in formatted strings, i.e. "url${digest}", this is done for you implicitly).
I'm frankly not familiar with String.fromCharCode, but I think it's looking for UTF-16 and not UTF-8 bits. I wrote a terminal example to show this, and how the outputs differ.
import 'dart:core';
import 'dart:convert';
import 'package:crypto/crypto.dart';
void main() {
const String password = "mypassword";
// hash the password
var bytes = utf8.encode(password);
var digest = sha256.convert(bytes);
// different formats
var bytesDigest = digest.bytes;
var hexDigest = digest.toString();
String url = "http://example_api.php?hash=";
print(url + hexDigest);
print(url + String.fromCharCodes(bytesDigest));
}
Output:
> dart test.dart
http://example_api.php?hash=89e01536ac207279409d4de1e5253e01f4a1769e696db0d6062ca9b8f56767c8
http://example_api.php?hash=à6¬ ry#Ö,©¸õggÈ
I am converting an Image to base64 and then I am putting it into the Map data structure, but when I convert Map into the JSON string, base64 string reduces and gets the reduced length response from the server. My question is why is Map reducing it?
"Message": "Invalid length for a Base-64 char array or string."
Converting Image file to Base64
var imageBytes = pfImage.readAsBytesSync();
String base64Image = base64Encode(imageBytes);
print("base64 is =$base64Image");
putting into the map
var petData = Map<String, dynamic>();
petData['user_id'] = 55;
petData['name'] = "sdf";
petData['breed'] = "dsf";
petData['StrImageBase64'] =base64Image != null ? base64Image : "";
converting to json string
print("map>>" + json.encode(map));
Some More Info
I have checked it locally as well by putting base64string into map and then print it, the result is in reduced string.
petData['StrImageBase64'] ="/9j/4AAQSkZJRgABAQAAAQABAAD/4RllRXhpZgAASUkqAAgAAAAHABoBBQABAAAAYgAAABsBBQABAAAAagAAACgBAwABAAAAAgAAADIBAgAUAAAAcgAAABMCAwABAAAAAQAAAGmHBAABAAAAhgAAACWIBAABAAAA6AAAACoBAABIAAAAAQAAAEgAAAABAAAAMjAxOToxMToxMCAwNTo1NTozMwAHAACQBwAEAAAAMDIxMAGRBwAEAAAAAQIDAAqSBQABAAAA4AAAAACgBwAEAAAAMDEwMAGgAwABAAAA//8AAAKgBAABAAAAAAUAAAOgBAABAAAAwAMAAAAAAAAkEwAA6AMAAAIABwAFAAMAAAAGAQAAHQACAAsAAAAeAQAAAAAAAAAAAAABAAAANwAAAAEAAAAfAAAAAQAAADIwMTk6MTE6MTAAAAIAAQIEAAEAAABIAQAAAgIEAAEAAAAVGAAAAAAAAP/Y/+AAEEpGSUYAAQEAAAEAAQAA/9sAQwAGBAUGBQQGBgUGBwcGCAoQCgoJCQoUDg8MEBcUGBgXFBYWGh0lHxobIxwWFiAsICMmJykqKRkfLTAtKDAlKCko/9sAQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo/8AAEQgA8AFAAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX2";
print("json.encode =${json.encode(petData)}");
UPDATED
It is not possible, that the map can reduce the value, or Json.decode(map) returns the reduced base64 string, Actually its VS code editor issue for not showing full base64 value.
I need to convert a cyrillic string to its urlencoded version using Windows-1251 encoding. For the following example string:
Моцарт
The correct result should be:
%CC%EE%F6%E0%F0%F2
I tried addingPercentEncoding(withAllowedCharacters:) but it doesn't work.
How to achieve the desired result in Swift?
NSString has a addingPercentEscapes(using:) method which allows to specify an arbitrary
encoding:
let text = "Моцарт"
if let encoded = (text as NSString).addingPercentEscapes(using: String.Encoding.windowsCP1251.rawValue) {
print(encoded)
// %CC%EE%F6%E0%F0%F2
}
However, this is deprecated as of iOS 9/macOS 10.11. It causes compiler warnings and may not be available in newer OS versions.
What you can do instead is to convert the string do Data with
the desired encoding,
and then convert each byte to the corresponding %NN sequence (using the approach from
How to convert Data to hex string in swift):
let text = "Моцарт"
if let data = text.data(using: .windowsCP1251) {
let encoded = data.map { String(format: "%%%02hhX", $0) }.joined()
print(encoded)
// %CC%EE%F6%E0%F0%F2
}