How to use hashFile method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

TimeStampController.js

Source: TimeStampController.js Github

copy
1/**
2 * TimeStampController
3 *
4 * @description :: Server-side actions for handling incoming requests.
5 * @help        :: See https://sailsjs.com/docs/concepts/actions
6 */
7
8const axios = require('axios');
9
10module.exports = {
11  
12    stamp: async function (req, res) {
13
14        if(!req.body.hashFile) return res.badRequest("Se esperaba el parametro hashFile");
15        const hashFile = req.body.hashFile;
16
17        await axios.post(sails.config.custom.tsaUrl + 'stamp', {
18            file_hash: hashFile
19        })
20        .then((response) => {               
21            if(response.status == 200 && response.data.tx_hash !== undefined){                                        
22                return res.status(200).json({
23                    status: 'success',
24                    infoStamp : response.data
25                });
26            } else {
27                return res.badRequest(res)
28            }
29        })
30        .catch((error) => {        
31            return res.badRequest(error)
32        });
33    },
34
35    verify: async function (req, res) {
36        if(!req.body.comprobanteOTS) return res.badRequest("Se esperaba el parametro comprobanteOTS");
37        if(!req.body.hashFile) return res.badRequest("Se esperaba el parametro hashFile");
38        
39        const comprobanteOTS = req.body.comprobanteOTS;        
40        const hashFile = req.body.hashFile;
41
42        const urlVerify = sails.config.custom.tsaUrl + 'verify/'+comprobanteOTS+'/'+hashFile;
43        
44        await axios.get(urlVerify)
45        .then( response => { 
46               
47            return res.status(200).json({
48                status: 'success',
49                verifyInfo: response.data
50            });
51        })
52        .catch(function (error) {            
53            return res.badRequest(error.toString());
54        }); 
55    }
56};
57
58
Full Screen

hash.js

Source: hash.js Github

copy
1var forge  = require('node-forge');
2var fs     = require('fs');
3var async  = require('async');
4var hash   = {};
5hash.file  = {};
6
7// =============================================================================
8// Base hashing methods
9// =============================================================================
10var hashDataSync = function(md, data) {
11  md.update(data);
12  return md.digest().toHex();
13};
14var hashData = async.asyncify(hashDataSync);
15
16var hashFileSync = function(md, file) {
17  // Get a base64 version of the file
18  // and give it to forge to obtain a
19  // forge Buffer
20  var buf64 = fs.readFileSync(file).toString('base64');
21  var fBuf = forge.util.decode64(buf64);
22  md.update(fBuf);
23  return md.digest().toHex();
24};
25var hashFile = async.asyncify(hashFileSync);
26
27// =============================================================================
28// Hash specific methods
29// =============================================================================
30hash.md5 = function(data, cb) {
31  hashData(forge.md.md5.create(), data, cb);
32};
33hash.file.md5 = function(file, cb) {
34  hashFile(forge.md.md5.create(), file, cb);
35};
36
37hash.sha1 = function(data, cb) {
38  hashData(forge.md.sha1.create(), data, cb);
39};
40hash.file.sha1 = function(file, cb) {
41  hashFile(forge.md.sha1.create(), file, cb);
42};
43
44hash.sha256 = function(data, cb) {
45  hashData(forge.md.sha256.create(), data, cb);
46};
47hash.file.sha256 = function(file, cb) {
48  hashFile(forge.md.sha256.create(), file, cb);
49};
50
51hash.sha384 = function(data, cb) {
52  hashData(forge.md.sha384.create(), data, cb);
53};
54hash.file.sha384 = function(file, cb) {
55  hashFile(forge.md.sha384.create(), file, cb);
56};
57
58hash.sha512 = function(data, cb) {
59  hashData(forge.md.sha512.create(), data, cb);
60};
61hash.file.sha512 = function(file, cb) {
62  hashFile(forge.md.sha512.create(), file, cb);
63};
64
65hash.hmac = function(type, key, data, cb) {
66  var hmac = forge.hmac.create();
67  hmac.start(type, key);
68  hashData(hmac, data, cb);
69};
70hash.file.hmac = function(type, key, file, cb) {
71  var hmac = forge.hmac.create();
72  hmac.start(type, key);
73  hashFile(hmac, file, cb);
74};
75
76module.exports = hash;
77
Full Screen

electron-ffmpeg-lib.spec.js

Source: electron-ffmpeg-lib.spec.js Github

copy
1/********************************************************************************
2 * Copyright (C) 2019 Ericsson and others.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the Eclipse
10 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11 * with the GNU Classpath Exception which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 ********************************************************************************/
16
17const path = require('path');
18const { expect } = require('chai');
19
20const { hashFile } = require('../electron-ffmpeg-lib');
21
22function resource(...parts) {
23    return path.resolve(__dirname, 'test-resources', ...parts);
24}
25
26describe('ffmpeg utility functions', () => {
27
28    it('hashFile', async () => {
29        const [
30            hashA, hashB, hashC
31        ] = await Promise.all([
32            hashFile(resource('fileA.txt')),
33            hashFile(resource('fileB.txt')),
34            hashFile(resource('fileC.txt')),
35        ]);
36        expect(hashA.equals(hashC)).true;
37        expect(hashA.equals(hashB)).false;
38    });
39
40});
41
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)