The operation cannot be completed because the property 'HmacSHA256' is undefined

I'm attempting to encrypt an ID for use as parameters in Vue.js using Crypto-JS, but I keep encountering the error message: Cannot read property 'HmacSHA256' of undefined. I ran into the same issue when trying ASE and MD5. What am I doing wrong? Below is my code:

//in main.js
import CryptoJS from 'crypto-js'
Vue.use(CryptoJS)

//in my component
goToEvent (singleEvent) {
  const id = this.CryptoJS.HmacSHA256(singleEvent.id, 'mySecretKey').toString(this.CryptoJS.enc.Hex)
  this.$store.commit('SET_SELECTED_EVENT', singleEvent)
  this.$router.push(`/admin/events/event/${id}`)
}

Answer №1

To integrate encryption in your component, insert import CryptoJS from 'crypto-js' into the file. Then, apply CryptoJS.HmacSHA256(...) as shown below:

encryptData (data) {
  const encryptedData = CryptoJS.HmacSHA256(data, 'secretKey').toString(CryptoJS.enc.Hex)
  this.$store.dispatch('saveEncryptedData', encryptedData)
}

Note: Do not use Vue.use(CryptoJS) as 'crypto-js' is not a valid Vue plugin.

Alternatively, you can utilize a different Vue plugin called Vue CryptoJS. In your single file component, access the CryptoJS instance using any of the following methods:

Vue.CryptoJS
this.CryptoJS
this.$CryptoJS

Answer №2

Thank you so much for your assistance! I realized that I needed to convert my value to a JSON string and make some adjustments to ensure it was URL-friendly. Here is the final solution that worked for me:

import CryptoJS from 'crypto-js'

const key = 'mysecrete'

// Encryption
export function Encrypt(text) {
    const b64 = CryptoJS.AES.encrypt(JSON.stringify(text), key).toString()
    const e64 = CryptoJS.enc.Base64.parse(b64)
    return e64.toString(CryptoJS.enc.Hex)
}

// Decryption
export function Decrypt(text) {
    const reb64 = CryptoJS.enc.Hex.parse(text)
    const bytes = reb64.toString(CryptoJS.enc.Base64)
    const decrypt = CryptoJS.AES.decrypt(bytes, key)
    return decrypt.toString(CryptoJS.enc.Utf8)
}

Now I can easily import this solution into any component.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to send form data from a parent component to a child component?

Struggling to grasp Vue.js and how to pass data from a parent component to its child component? Not sure which direction to take after reading up on it. How can I transfer the input field name from the parent component to the child component upon pressing ...

Warning: MaxListenersExceededNotification may occur during the installation or creation of Node.js projects on macOS

Encountering a warning message while attempting to set up or generate Node.js projects on macOS (darwin): (node:80101) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxList ...

Transmit both image and JSON data in a single response using Node.js and Express

I'm in the process of building a web API using Node.js and Express and my goal is to send back both an image and JSON data in a single response. I came across a solution that involves specifying headers at , but I'm looking for a way to achieve t ...

Toggle button to create a fade in/out effect

Below is the HTML code snippet: <html> <head> <Script type = "text/javascript" src = "CprMdlrSrch.js"></Script> <link type="text/css"rel="stylesheet"href="CprMdlrSrch.css"/> </head> <body> <div id=" ...

The issue of Angular JQuery Datepicker failing to set the MinDate upon initialization

In my AngularJS project, I am using JQuery UI's Datepicker for the Date From and Date To fields. I have successfully bound the value to the model using a directive, and I have also implemented logic in the OnSelect function to ensure that the Date To ...

Issue with multiple dropdown menus not closing when clicked on

The current implementation provides the functionality to convert select boxes into list items for styling purposes. However, a drawback of the current setup is that once a dropdown is opened, it can only be closed by clicking on the document or another dr ...

Tips for controlling the embla carousel based on breakpoints in a React application

Trying to toggle the Embla Carousel on and off based on different breakpoints using the React version. I have attempted to initialize it for mobile and destroy it for tablet upwards, but it seems like the reinitialization is not working as expected. I su ...

Tips for resolving the chakra-ui theme issue with loading input background

I've been working on applying a theme to my Next.js app using chakra-ui, but I've been facing an issue that I can't seem to resolve. Currently, when the /form route is loaded, the theme is set to light. After changing it to dark, the theme i ...

How to simultaneously update two state array objects in React

Below are the elements in the state array: const [items, setItems] = useState([ { id: 1, completed: true }, { key: 2, complete: true }, { key: 3, complete: true } ]) I want to add a new object and change the ...

Can someone explain the method for displaying or concealing a menu based on scrolling direction?

https://i.stack.imgur.com/tpDx0.jpg I want to hide this menu when scrolling down and show it when scrolling up. The code for my menu bot is: <script> var previousScroll = 0; $(window).scroll(function(event){ v ...

What is the best way to sort and organize this JSON data?

There is an array of JSON objects named events that contains fields such as eventId, eventName, and the date of the event. [{event1}, {event2}, {event3}]; The goal is to iterate through this array and filter out events that occur on the same day. The des ...

What is the best way to dynamically alter HTML elements on my webpage?

I have a selection of radio buttons on my webpage: <form ...> <input type="radio" name="people" checked> Member <input type="radio" name="people"> Manager <input type="radio" name="people"> Supervisor <!-- Here is t ...

Tips for concealing broken images in jQuery/JavaScript when generating dynamic content

Is there a way to hide the broken images in this dynamically generated HTML code using JavaScript? Unfortunately, I don't have access to the source code itself. I'm new to JQuery and could really use some assistance. Below is the snippet of the ...

What is the best way to retrieve JSON data values for buttons that have been clicked and files that have

Is there a way to retrieve JSON data only for clicked buttons and uploaded files? Currently, I am receiving all button values, whether clicked or not, in one instance, and file paths with fake paths like C:\\fakepath\\test.txt in anothe ...

Is there a way to simulate pressing the ENTER/RETURN key using JavaScript executor in Selenium using Python?

Greetings everyone, I am a newcomer to Python Selenium and currently working on automating a website. However, I have encountered an issue with the search text box of the website as it does not feature any clickable buttons once the text is entered. Here ...

How to implement a service function to handle $http responses in a controller

Is it possible to use $http only for my service and not the controller? I am getting undefined in my console.log when trying to display data in a $scope. Here is my code: app.controller('adminControl', ['$scope','$routeParams&apo ...

Dynamics of Rails and the power of jQuery with ajaxStop

For my Rails 5.2 project, I have included the gems 'jquery-rails' and 'jquery-ui-rails'. I am attempting to utilize the .ajaxStop( handler ) handler as outlined in the documentation found here: https://api.jquery.com/ajaxStop/#ajaxStop ...

Leverage Next.js 13 to utilize both search parameters and route parameters in a static site

I'm currently in the process of setting up a blog using the Next.js application directory. I have a route that is structured as follows: /blogs/category/[name] This route displays all blogs within a specific category and utilizes an SSG page, implem ...

Determining html column values using a related column and user input

Is there a way to populate an HTML table column (using Javascript or jQuery) based on the values in another column and an input field? For instance, if I input the number 115 into the field, then the advance column should display a value of 1 for each ath ...

Please proceed with submitting your choices in the order that you have selected

My goal is to submit options from a dropdown list in the order they are selected, rather than in the order they appear in the list. How can I achieve this? Here is the HTML code for the dropdown: < select style = "padding: 1em;" name = "skills" multi ...