How do I base64 encode a string in cloud code? The value is for a HTTP Basic Auth.
I have tried the following two approaches and I had no success.
var string = 'AQXTTPmj-boT_yDEPQXg9ezIOIM7O:EMx6RLr8jF3S6YYo-X4bZ';
var buffer1 = new Buffer(string, 'base64');
var b3 = buffer1.toString('base64');
console.log(b3);
var string = 'AQXTTPmj-boT_yDEPQXg9ezIOIM7O:EMx6RLr8jF3S6YYo-X4bZ';
var encodeString = Base64.encode(string);
console.log(encodeString);
You send your string to the Buffer constructor and use toString method to convert it to base64 like this:
var string = 'AQXTTPmj-boT_yDEPQXg9ezIOIM7O:EMx6RLr8jF3S6YYo-X4bZ';
var buffer1 = new Buffer(string);
var b3 = buffer1.toString('base64');
console.log(b3);
Also make sure you put var Buffer = require('buffer').Buffer; on top of your main.js file.
Related
I need to get the integer value "maximum drawdown" from this website link =https://www.mql5.com/en/signals/1414510,
from google chrome i tried copy the xpath
//*[#id="radarChart"]/svg/text[8]/tspan[2]
then in google sheet, i write:
=importxml(https://www.mql5.com/en/signals/1414510, "//*[#id='radarChart']/svg/text[8]/tspan[2]")
It cant retrieve the value,
How to write the correct xpath to the value?
thanks
by xpath
Try for https://www.mql5.com/en/signals/1409351
=regexextract(importxml(A1,"//div[#class='s-data-columns hoverable']"),"\((.*)\)")
i highly recommand to test first this formula to have an overview
=importxml(A1,"//div[#class='s-data-columns hoverable']")
by json
the second way could be to parse the json contained in the source (var radarChart)
function radarChart(url){
var source = UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getContentText().replace(/(\r\n|\n|\r|\t| )/gm,"")
var data = source.split(`new svgRadarChart($('radarChart'),[`)[1].split(']')[0]
var values = (data.match(/value : ([^,]+)/g))
var names = (data.match(/name : ([^,]+)/g))
var val
for (var i=0;i<names.length;i++){if (names[i]==`name : 'Maximum drawdown'`){val=values[i]}}
return (val)
}
or
function radarChart(url){
var source = UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getContentText().replace(/(\r\n|\n|\r|\t| )/gm,"")
var data = source.split(`new svgRadarChart($('radarChart'),[`)[1].split(']')[0]
var values = (data.match(/value : ([^,]+)/g))
var names = (data.match(/name : ([^,]+)/g))
var result = []
for (var i=0;i<names.length;i++){
result.push([names[i].replace('name : ','').replace(/'/g,''),values[i].replace('value : ','')])
}
return (result)
}
How to Spilt this paymentUrl.
const _paymentUrl =
'bitcoin:3QF3iP4PZPw51qB5w6Jpo8j7P4AXyS83ra?amount=0.00107000';
TO Get
{Address: "3QF3iP4PZPw51qB5w6Jpo8j7P4AXyS83ra", Amount: "0.00107000"}
It looks like a URI, and is named like a URI, so try using the Uri class:
const _paymentUrl =
'bitcoin:3QF3iP4PZPw51qB5w6Jpo8j7P4AXyS83ra?amount=0.00107000';
var bcUri = Uri.parse(_paymentUrl);
var address = bcUri.path;
var amount = bcUri.queryParameters["amount"];
var map = {"Address": address, "Amount": amount};
it seems the ? is always there, so you can split it based on it like this:
const _paymentUrl = 'bitcoin:3QF3iP4PZPw51qB5w6Jpo8j7P4AXyS83ra?amount=0.00107000';
List<String> splitPayment = _paymentUrl.split('?');
String bitcoin = splitPayment[0];
String amount = splitPayment[1];
I'm trying to use pointy castle to generate a public and private keypair using the secp256k1 curve. I think i have successfully created an AsymmetricKeyPair composed of an ECPrivateKey and ECPublicKey but i can't obtain their corresponding hex strings (something like this:
private:
ee792658c8eb1f8c3d2010ee6bc2ea328bb584fbecbfb17cf0f9103d122a8716,
public:
041b3f87beb2559aa3ca1c1d9ebb9447e4842d21cf0c70db103acc0db27ea8c27536fc2b1405b8a16a460ca089b01de8c556825927b4890b7236e357787f3e6d54).
When i try to print the keys, all i get is "Instance of 'ECPrivateKey'" and "Instance of 'ECPublicKey'" whether i use .toString() or not.
I have been looking around everywhere for a way to do this but i can't find one, is it even possible?
Here's my code:
SecureRandom secureRandom = new SecureRandom("Fortuna");
var random = new Random.secure();
List<int> seeds = [];
for (int i = 0; i < 32; i++) {
seeds.add(random.nextInt(255));
}
secureRandom.seed(new KeyParameter(new Uint8List.fromList(seeds)));
var domainParams = new ECDomainParameters("secp256k1");
var ecParams = new ECKeyGeneratorParameters(domainParams);
var params = new ParametersWithRandom<ECKeyGeneratorParameters>(
ecParams, secureRandom);
var keyGenerator = new ECKeyGenerator();
keyGenerator.init(params);
AsymmetricKeyPair keypair = keyGenerator.generateKeyPair();
ECPrivateKey privateKey = keypair.privateKey;
ECPublicKey publicKey = keypair.publicKey;
print(privateKey);
print(privateKey.toString());
print(publicKey);
print(publicKey.toString());
The private key and public point are member variables of their respective keys:
ECPrivateKey privateKey = keypair.privateKey;
ECPublicKey publicKey = keypair.publicKey;
// in decimal
print(privateKey.d);
print(publicKey.Q.x);
print(publicKey.Q.y);
// in hex
print(privateKey.d.toRadixString(16));
print(publicKey.Q.x.toBigInteger().toRadixString(16));
print(publicKey.Q.y.toBigInteger().toRadixString(16));
fileReader = new StreamReader(new IsolatedStorageFileStream("textfiles\\easy.txt", FileMode.Open, filestorage));
var obj = App.Current as App;
var lines = fileReader.ReadToEnd();
obj.textfile = (string[])fileReader.ReadToEnd();
Getting an error
"cannot convert string to string[]" in obj.textfile =
(string[])fileReader.ReadToEnd();
If you want an array of lines from a single string, you can do
var arrayOfLines = lines.split("\n");
I'm reading an NFC tag in my Adobe AIR mobile app. The data is read as a ByteArray, but I'm having difficulty pulling the full text. The sample text on the tag is "http://www.google.com"
Using this method, I get a portion of the String "http://www.goog", but not all of it. I'm assuming because each character is not a single byte:
private static function convertToString(byte_array : ByteArray) : String {
var arr : Array = [];
for (var i : Number = 1 ; i <= byte_array.bytesAvailable; i++) {
arr.push(byte_array.readUTFBytes(i));
}
var finalString : String = "";
for (var t : Number = 0; t < arr.length;t++) {
finalString = finalString + arr[t].toString();
}
return finalString;
}
I've also tried the method below, but it returns null:
bytes.readUTF();
I'm wondering if I need to convert the byteArray to a base64 string and then decode that. It seems like an extra step, but that's how I've done it before when sending data to/from a server using AMFPHP.
Thanks in advance for any input.
You could even simplify this code by simply calling
private static function convertToString(bytes:ByteArray):String {
bytes.position = 0;
var str:String = bytes.readUTFBytes(bytes.length);
return str;
}
This way you will read all contents of the bytearray in one single method call into your destination string.
Figured it out in the code below.
There were 2 errors, plus some cleanup:
private static function convertToString(bytes : ByteArray) : String {
bytes.position = 0;
var str : String = '';
while (bytes.bytesAvailable > 0) {
str += bytes.readUTFBytes(1);
}
return str;
}
the "bytesAvailable" property decreases as you read from the ByteArray. Here, I'm checking if the bytes > 0 instead of the length
the "readUTFBytes" method takes a length parameter (not position). Position is automatically updated as you read from the ByteArray. I'm passing in "1" instead of "i"