I need to check the md5 of an image in django and in javascript. However I don't obtain the same results. The django code give me the same md5 as md5sum on a terminal.
Django code :
file0 = request.FILES.get('file')
buffer0 = file0.read()
print hashlib.md5(buffer0).hexdigest() //9f982242d24ab97a7254edd7e28e3921
I tried two javascript md5 library, which give me the same results ( https://github.com/blueimp/JavaScript-MD5 and https://github.com/sytelus/CryptoJS ).
I have tried different methods (readAsArrayBuffer, readAsBinaryString, ...) and none of them give me the same md5 as python and md5sum.
Javascript :
var reader = new FileReaderSync();
var datas = reader.readAsArrayBuffer(file);
console.log(md5(datas)); //060e4e9e30bcb9ae675a80328a87a687
var string0 = reader.readAsBinaryString(file);
console.log(md5(string0)); //2e4cac0a23ddf95683c6538d64b26e21
console.log(CryptoJS.MD5(string0).toString(CryptoJS.enc.Hex)); //2e4cac0a23ddf95683c6538d64b26e21
var string1 = reader.readAsText(file,'ascii');
console.log(md5(string1)); //329c4271b8eda786213b2468e378b251
console.log(CryptoJS.MD5(string1).toString(CryptoJS.enc.Hex));//329c4271b8eda786213b2468e378b251
var view = new Uint8Array(datas);
var str = ""
for (var i=0, strLen=view.length; i < strLen; i++) {
str+= String.fromCharCode(view[i]);
}
console.log(md5(str)); //2e4cac0a23ddf95683c6538d64b26e21
console.log(CryptoJS.MD5(string1).toString(CryptoJS.enc.Hex));
I think the problem come from how I read the file in JS.
I used SparkMD5 (https://github.com/satazor/js-spark-md5) which can worked directly on an array buffer and it give me the same md5 than in python.
Related
I work as legal support in litigation. I am not that clued up on scripting, but have managed to adapt a few google searches to perform various tasks for in Adobe.
What I need is help with what I think should be a simple script to read through a PDF and extract Document IDs. They are enclosed in square brackets, so I just need to extract all text between square brackets to a text or CSV file. I have tried using the ChatGPT bot but that hasnt been very successful. This is the code it has given me
// Open the PDF file
var filePath = "/path/to/your/file.pdf";
var doc = app.open(filePath);
// Get the number of pages
var numPages = doc.numPages;
// Create an array to hold the results
var results = [];
// Loop through each page and extract text between square brackets
for (var i = 0; i < numPages; i++) {
var page = doc.getPageNthWordQuads(i);
for (var j = 0; j < page.length; j++) {
var word = page[j];
var text = word[4];
// Check if the text is between square brackets
if (text.startsWith("[") && text.endsWith("]")) {
// Remove the brackets and add the text to the results array
results.push(text.slice(1, -1));
}
}
}
// Save the results to a text file
var outputPath = "/path/to/your/output/file.txt";
var outputFile = new File(outputPath);
outputFile.open("w");
outputFile.write(results.join("\n"));
outputFile.close();
// Close the PDF file
doc.close();
I ran the script, with my file directory, not the placeholder in the script, but nothing happened. No error or anything
I am using a work PC so I cant install python or any other program, hence the need for Java or possibly powershell if that will work
Can anyone help me?
Actually I realised i could do this using the Evermap plugin. Highlight text by pattern - [(.*?)], then extract highlighted text.
This can be achieved with pdf.js library: below example shows if specific text in the first page but can be furhter extended to check the whole pdf. Hope this helps!
// Load PDF.js library
const pdfjsLib = require('pdfjs-dist');
// Load PDF file
const url = 'path/to/pdf/file.pdf';
const loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdf) {
// Load the first page
pdf.getPage(1).then(function(page) {
// Get the text content of the page
page.getTextContent().then(function(textContent) {
// Iterate through each text item
for (let i = 0; i < textContent.items.length; i++) {
const item = textContent.items[i];
// Check if the text item matches your criteria
if (item.str.includes('specific text')) {
console.log(item.str);
}
}
});
});
});
You can get rid of the entire for loop and just use a regular expression with String.match():
const data = "This is the text ofa pdf file [documentid1] and [documentid2].";
const matches = data.match(/(?<=\[).*?(?=\])/gs);
console.log(matches);
I have a CSR and I can parse all the data with pkijs.org lib, but I have no luck to parse alternative names data. How is it possible to do with a javascript? Some other libs can be in use, I guess, do you know one?
Following the docs of CertificationRequest class provided by pkijs here https://pkijs.org/docs/classes/CertificationRequest.html. We can see that the structure of a CSR. The subject alternative name will be stored in attributes propery of CertificationRequest object. But the structure inside of attributes is quite complex to make it as plain text. This is my code used to print out the subject alternative name
const pkijs = require('pkijs');
const utils = require("pvtsutils");
const asn1js = require("asn1js");
let base64 = "<your_csr_in_base64>"
let csrraw = utils.Convert.FromBase64(base64);
console.log(csrraw)
const pkcs10 = pkijs.CertificationRequest.fromBER(csrraw);
let seq = pkcs10.attributes[0].values[0];
let exts = pkijs.Extensions.fromBER(seq.toBER(false));
console.log(exts);
var san = getExtentionsForSANFromExtensions(exts);
console.log(san)
if (san != undefined) {
san.names.forEach(element => {
console.log(element.type + " = " + element.value)
});
}
function getExtentionsForSANFromExtensions(exts){
for (var i = 0 ; i< exts.extensions.length; i++) {
var ext = exts.extensions[i];
if(ext.extnID == '2.5.29.17') {
var octetString = asn1js.fromBER(ext.extnValue.toBER(false)).result;
return pkijs.GeneralNames.fromBER(octetString.getValue());
}
}
}
I've tested this code and it works properly with CSR generated by Keystore Explorer. Have not tested with another tool to generate CSR that supports subject alternative names.
Cheers!
If you have a CSR and need to extract the alternative names data from it, you can use the following command:
openssl req -in csr.pem -noout -text
This will print out the entire CSR, including the alternative names data.
I am working on a tool for manual classification which changes the property of certain dot(color in my case) chosen in a scatter plot by bokeh. I changed the source data in callback by s.data = d2 and s.change.emit() but both failed. I thought such operation will change source.data, but when I print source.data, actually nothing happens.
The dots' color in the plot changes as expected though.
Here is my related code:
DF = pd.read_csv(csv_path)
s = ColumnDataSource(DF_file)
p = figure(plot_width=500, plot_height=500, tooltips=TOOLTIPS,tools="lasso_select, tap", title="manual classification")
circles = p.circle('x', 'y', color='color', size=10, source=s, line_alpha=0.6,fill_alpha=0.6)
s.callback = CustomJS(args=dict(s1=s), code="""
var inds = cb_obj.selected.indices;
var d1 = s1.data;
for (var i = 0; i < inds.length; i++)
{d1['color'][inds[i]] = 'green';}
s1.change.emit();
""")
Both print(s.data) and the csv file saved from s.to_csv(xxx) shows no change to the original input data.
Also, I wonder how does callback work to change the plot's data while leave the data in python unchanged when the data in python is the data passed to it in args=(s1=s).
I have searched for some possible methods and found this answer in https://discourse.bokeh.org/t/getting-selected-glyph-data-properties-via-customjs/4311/5?u=1113
when the Bokeh JS object is instantiated it uses the Python objects as sources for data but then is essentially disconnected from them - so updates to the JS model are not propagated back to their Python parents.
While the discussion in this webpage also proposed a workaround using IPython.notebook.kernel.execute to create or overwrite a variable in the Python. It can only be used in a notebook frontend(I found this workaround only works when use output_notebook() in the code).
Then here is my new code to change the original data in python:
s.callback = CustomJS(args=dict(s1=s1), code="""
var inds = s1.selected.indices;
var d1 = s1.data;
for (var i = 0; i < inds.length; i++)
{
d1['color'][inds[i]] = 'red';
var index = inds[i];
var command = "s1.data['color'][" + index + "] = red";
var kernel = IPython.notebook.kernel;
kernel.execute(command);
}
s1.change.emit();;
""")
I'm learning to read javascript variable using Selenium WebDriver (latest version). Sometimes it works, sometimes not. Below is my try on whoscored.com and it keeps showing error
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://www.whoscored.com/Regions/81/Tournaments/3/Germany-Bundesliga");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
var tournament = wait.Until(ExpectedConditions.ElementExists(By.Id("tournament-fixture-wrapper")));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
var obj = (object)js.ExecuteScript("return window.allRegions;"); //always return error 'Additional information: Unable to cast object of type 'System.Int64' to type 'System.String'.
}
I think you should change
var obj = (object)js.ExecuteScript("return window.allRegions;");
to
List<object> list = js.ExecuteScript("return window.allRegions;") as List<object>;
since, return window.allRegions; does not return a string but array of objects.
Edit
Just went through the page and looks like window.allRegions returns a List of json objects. And, does feel like creating a list of json object can be unwanted overwhelming of programming. I suggest you to narrow down the goal either with modifying the javascript or performing some filtering like following.
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
var tournament = wait.Until(ExpectedConditions.ElementExists(By.Id("tournament-fixture-wrapper")));
IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
//getting count of regions
long count = (long)js.ExecuteScript("return window.allRegions.length;");
for (int i = 0; i < count; i++)
{
//grab the name of countries if that's what you wanted
string name = js.ExecuteScript("return window.allRegions[" + i + "].name;") as string;
Console.WriteLine(name);
}
Print:
Africa
Albania
Algeria
...
Zambia
Zimbabwe
I've been developing a simple application using Adobe AIR, the HTML and javascript version.
The application submits a form to an online URL.
The values of the forms are JSON strings.
I'm using this function to submit the data:
function fetchStudents()
{
var stmt = new air.SQLStatement();
stmt.sqlConnection = conn;
stmt.text = "SELECT * FROM studentsTable2 WHERE deleted='0'";
stmt.addEventListener(air.SQLEvent.RESULT, function(event){
var result = event.target.getResult();
sync_students = JSON.stringify(result.data);
fetchCourses();
});
stmt.addEventListener(air.SQLErrorEvent.ERROR, errorHandler);
stmt.execute();
}
JSON.stringify works when I test the application in DREAMWEAVER by using: preview in ADOBE AIR.
sync_students is then a JSON string filled with all the data from the table correctly formatted.
But when I have created the AIR file and installed the application and run it, it no longer works.
sync_students is then a JSON string but it is completely empty... [{},{},{}]
I have read around a lot and seen suggestions to use JSON2.js etc and I have tried these but I haven't been successful.
This is driving me crazy, any help would be greatly appreciated.
Thanks so much in advance!
Have a look at the accepted answer of this question. The problem here was that garbage collection reclaims some var 'too soon' : in other words, the scope is baaad. :
AIR Sqlite: SQLEvent.RESULT not firing, but statement IS executing properly
EDIT : All this is a question of scope. You should look into 'javascript scope' and 'javascript closure' keywords to have a better understanding of this (if i may suggest).
below just a (very) short summary about ONE WAY to define a 'class' in javascript :
var MyNameSpace = {};
var MyNameSpace.SchoolDataSource = function() {
this.publicMember = 2;
this.publicFunction = function(x) {
var newValue = this.publicMember + _privateMemberOne + x;
return _privateFunctionMul2(newValue);
} ;
var _privateMemberOne = 1;
var _privateFunctionMul2 = function (y) { return 2*y; } ;
};
var mySchoolDataSource = new MyNameSpace.SchoolDataSource();
mySchoolDataSource.publicFunction(3); // ok. (returns (3+2+1) * 2 = 12)
var bar = mySchoolDataSource.publicMember; // ok. ( === 2)
mySchoolDataSource._privateFunctionMul2(4); does not work, which is what we want.
var foo= mySchoolDataSource._privateMemberOne; // does not work, which is what we want.