npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@filipigustavo/enc-dec

v0.3.0

Published

A simple library to hide values in localStorage easily.

Downloads

22

Readme

Enc-Dec

A simple library to hide values in localStorage easily.

Usage

The useHash hook returns enc and dec methods. Use it to save encrypted data and get it from localStorage.

enc(key: string, value: any)

dec(key: string)

  • You can have one or more instances in your application using namespaces in useHash.
  • You can change the way useHash generate security hash.

Example: simple usage

import { useState } from 'react'
import { useHash } from '@filipigustavo/enc-dec'

function App() {
  const { enc, dec } = useHash()
  const [raw, setRaw] = useState('')
  const [decrypted, setDecrypted] = useState('')
  
  const handleEnc = () => enc('local-storage-key', raw)

  const handleDec = () => {
    const val = dec('local-storage-key')
    setDecrypted(val)
  }

  return (
    <div>
      <h1>Enc/Dec</h1>

      <div>
        <input value={raw} onChange={(ev) => setRaw(ev.target.value)} />
        <button onClick={handleEnc}>Encrypt data</button>
        <button onClick={handleDec}>Decrypt data</button>
        <br />
        Decrypted Value: {decrypted}
      </div>
    </div>
  )
}

export default App

If you want another namespaced instance, just pass a prefix as first parameter of useHash. You can have so many instances you want.

// default usage
const { enc, dec } = useHash()
// using with a namespace
const { enc: enc2, dec: dec2 } = useHash('my_prefix-')

IMPORTANT: Don't forget to always use the same prefix to get data from this new namespace.

Changing the way useHash generates security hash

To do it, you should make a class that extends AbstractGenerator with generateHashParts and handleHash methods.

AbstractGenerator accepts a Generic type. generateHashParts should return the same type declared in the class and handleHash accepts a parameter with these type and always returns a string.

AbstractGenerator<H>

This is the base class that works with hashs. You should extend it and implement generateHashParts and handleHash methods.

generateHashParts(): H

This method generates the base to make the real hash. This value will be persisted in localStorage.

handleHash(hash: H): string

This method takes the value generated by generateHashParts and transforms it in the real hash used to encrypt/decrypt data. This value WILL NOT be persisted in localStorage.

class NewHash extends AbstractGenerator<string> {
  generateHashParts = (): string => {
    // You can do whatever you want here.
    // This value will be saved in localStorage as the base to real hash.
    return "hello"
  }

  handleHash = (rawHash: string): string => {
    // You can modify the rawHash the way you want to generate the real hash.
    // This new value will be used to encrypt/decrypt data.
    const hash = rawHash.split('').reverse().join('-')
    return hash
  };
}