CryptoJs常规密码加密demo

 更新时间:2023年06月02日 11:21:52   作者:freeman_Tian  
这篇文章主要为大家介绍了CryptoJs常规密码加密的实现demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

常规密码加密

样例:

import { CryptoJs } from "@/js/crypto.js";
 let passwordVal = CryptoJs().Base64.stringify(CryptoJs().HmacSha256(this.password, 'Meiauto$'));

crypto.js 源码

export const CryptoJs = () => {
    const CryptoJs = {}
    class Base {
        /**
         * Extends this object and runs the init method.
         * Arguments to create() will be passed to init().
         *
         * @return {Object} The new object.
         *
         * @static
         *
         * @example
         *
         *     var instance = MyType.create();
         */
        static create(...args) {
            return new this(...args);
        }
        /**
         * Copies properties into this object.
         *
         * @param {Object} properties The properties to mix in.
         *
         * @example
         *
         *     MyType.mixIn({
         *         field: 'value'
         *     });
         */
        mixIn(properties) {
            return Object.assign(this, properties);
        }
        /**
         * Creates a copy of this object.
         *
         * @return {Object} The clone.
         *
         * @example
         *
         *     var clone = instance.clone();
         */
        clone() {
            const clone = new this.constructor();
            Object.assign(clone, this);
            return clone;
        }
    }
    class WordArray extends Base {
        /**
         * Initializes a newly created word array.
         *
         * @param {Array} words (Optional) An array of 32-bit words.
         * @param {number} sigBytes (Optional) The number of significant bytes in the words.
         *
         * @example
         *
         *     var wordArray = CryptoJS.lib.WordArray.create();
         *     var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
         *     var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
         */
        constructor(words = [], sigBytes = words.length * 4) {
            super();
            let typedArray = words;
            // Convert buffers to uint8
            if (typedArray instanceof ArrayBuffer) {
                typedArray = new Uint8Array(typedArray);
            }
            // Convert other array views to uint8
            if (
                typedArray instanceof Int8Array
                || typedArray instanceof Uint8ClampedArray
                || typedArray instanceof Int16Array
                || typedArray instanceof Uint16Array
                || typedArray instanceof Int32Array
                || typedArray instanceof Uint32Array
                || typedArray instanceof Float32Array
                || typedArray instanceof Float64Array
            ) {
                typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
            }
            // Handle Uint8Array
            if (typedArray instanceof Uint8Array) {
                // Shortcut
                const typedArrayByteLength = typedArray.byteLength;
                // Extract bytes
                const _words = [];
                for (let i = 0; i < typedArrayByteLength; i += 1) {
                    _words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8);
                }
                // Initialize this word array
                this.words = _words;
                this.sigBytes = typedArrayByteLength;
            } else {
                // Else call normal init
                this.words = words;
                this.sigBytes = sigBytes;
            }
        }
        /**
         * Creates a word array filled with random bytes.
         *
         * @param {number} nBytes The number of random bytes to generate.
         *
         * @return {WordArray} The random word array.
         *
         * @static
         *
         * @example
         *
         *     var wordArray = CryptoJS.lib.WordArray.random(16);
         */
        static random(nBytes) {
            const words = [];
            const r = (m_w) => {
                let _m_w = m_w;
                let _m_z = 0x3ade68b1;
                const mask = 0xffffffff;
                return () => {
                    _m_z = (0x9069 * (_m_z & 0xFFFF) + (_m_z >> 0x10)) & mask;
                    _m_w = (0x4650 * (_m_w & 0xFFFF) + (_m_w >> 0x10)) & mask;
                    let result = ((_m_z << 0x10) + _m_w) & mask;
                    result /= 0x100000000;
                    result += 0.5;
                    return result * (Math.random() > 0.5 ? 1 : -1);
                };
            };
            for (let i = 0, rcache; i < nBytes; i += 4) {
                const _r = r((rcache || Math.random()) * 0x100000000);
                rcache = _r() * 0x3ade67b7;
                words.push((_r() * 0x100000000) | 0);
            }
            return new WordArray(words, nBytes);
        }
        /**
         * Converts this word array to a string.
         *
         * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
         *
         * @return {string} The stringified word array.
         *
         * @example
         *
         *     var string = wordArray + '';
         *     var string = wordArray.toString();
         *     var string = wordArray.toString(CryptoJS.enc.Utf8);
         */
        toString(encoder = Hex) {
            return encoder.stringify(this);
        }
        /**
         * Concatenates a word array to this word array.
         *
         * @param {WordArray} wordArray The word array to append.
         *
         * @return {WordArray} This word array.
         *
         * @example
         *
         *     wordArray1.concat(wordArray2);
         */
        concat(wordArray) {
            // Shortcuts
            const thisWords = this.words;
            const thatWords = wordArray.words;
            const thisSigBytes = this.sigBytes;
            const thatSigBytes = wordArray.sigBytes;
            // Clamp excess bits
            this.clamp();
            // Concat
            if (thisSigBytes % 4) {
                // Copy one byte at a time
                for (let i = 0; i < thatSigBytes; i += 1) {
                    const thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
                    thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
                }
            } else {
                // Copy one word at a time
                for (let i = 0; i < thatSigBytes; i += 4) {
                    thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
                }
            }
            this.sigBytes += thatSigBytes;
            // Chainable
            return this;
        }
        /**
         * Removes insignificant bits.
         *
         * @example
         *
         *     wordArray.clamp();
         */
        clamp() {
            // Shortcuts
            const { words, sigBytes } = this;
            // Clamp
            words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
            words.length = Math.ceil(sigBytes / 4);
        }
        /**
         * Creates a copy of this word array.
         *
         * @return {WordArray} The clone.
         *
         * @example
         *
         *     var clone = wordArray.clone();
         */
        clone() {
            const clone = super.clone.call(this);
            clone.words = this.words.slice(0);
            return clone;
        }
    }
    const Latin1 = {
        /**
         * Converts a word array to a Latin1 string.
         *
         * @param {WordArray} wordArray The word array.
         *
         * @return {string} The Latin1 string.
         *
         * @static
         *
         * @example
         *
         *     var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
         */
        stringify(wordArray) {
            // Shortcuts
            const { words, sigBytes } = wordArray;
            // Convert
            const latin1Chars = [];
            for (let i = 0; i < sigBytes; i += 1) {
                const bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
                latin1Chars.push(String.fromCharCode(bite));
            }
            return latin1Chars.join('');
        },
        /**
         * Converts a Latin1 string to a word array.
         *
         * @param {string} latin1Str The Latin1 string.
         *
         * @return {WordArray} The word array.
         *
         * @static
         *
         * @example
         *
         *     var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
         */
        parse(latin1Str) {
            // Shortcut
            const latin1StrLength = latin1Str.length;
            // Convert
            const words = [];
            for (let i = 0; i < latin1StrLength; i += 1) {
                words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
            }
            return new WordArray(words, latin1StrLength);
        },
    };
    const Utf8 = {
        /**
         * Converts a word array to a UTF-8 string.
         *
         * @param {WordArray} wordArray The word array.
         *
         * @return {string} The UTF-8 string.
         *
         * @static
         *
         * @example
         *
         *     var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
         */
        stringify(wordArray) {
            try {
                return decodeURIComponent(escape(Latin1.stringify(wordArray)));
            } catch (e) {
                throw new Error('Malformed UTF-8 data');
            }
        },
        /**
         * Converts a UTF-8 string to a word array.
         *
         * @param {string} utf8Str The UTF-8 string.
         *
         * @return {WordArray} The word array.
         *
         * @static
         *
         * @example
         *
         *     var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
         */
        parse(utf8Str) {
            return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
        },
    };
    class BufferedBlockAlgorithm extends Base {
        constructor() {
            super();
            this._minBufferSize = 0;
        }
        /**
         * Resets this block algorithm's data buffer to its initial state.
         *
         * @example
         *
         *     bufferedBlockAlgorithm.reset();
         */
        reset() {
            // Initial values
            this._data = new WordArray();
            this._nDataBytes = 0;
        }
        /**
         * Adds new data to this block algorithm's buffer.
         *
         * @param {WordArray|string} data
         *
         *     The data to append. Strings are converted to a WordArray using UTF-8.
         *
         * @example
         *
         *     bufferedBlockAlgorithm._append('data');
         *     bufferedBlockAlgorithm._append(wordArray);
         */
        _append(data) {
            let m_data = data;
            // Convert string to WordArray, else assume WordArray already
            if (typeof m_data === 'string') {
                m_data = Utf8.parse(m_data);
            }
            // Append
            this._data.concat(m_data);
            this._nDataBytes += m_data.sigBytes;
        }
        /**
         * Processes available data blocks.
         *
         * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
         *
         * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
         *
         * @return {WordArray} The processed data.
         *
         * @example
         *
         *     var processedData = bufferedBlockAlgorithm._process();
         *     var processedData = bufferedBlockAlgorithm._process(!!'flush');
         */
        _process(doFlush) {
            let processedWords;
            // Shortcuts
            const { _data: data, blockSize } = this;
            const dataWords = data.words;
            const dataSigBytes = data.sigBytes;
            const blockSizeBytes = blockSize * 4;
            // Count blocks ready
            let nBlocksReady = dataSigBytes / blockSizeBytes;
            if (doFlush) {
                // Round up to include partial blocks
                nBlocksReady = Math.ceil(nBlocksReady);
            } else {
                // Round down to include only full blocks,
                // less the number of blocks that must remain in the buffer
                nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
            }
            // Count words ready
            const nWordsReady = nBlocksReady * blockSize;
            // Count bytes ready
            const nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
            // Process blocks
            if (nWordsReady) {
                for (let offset = 0; offset < nWordsReady; offset += blockSize) {
                    // Perform concrete-algorithm logic
                    this._doProcessBlock(dataWords, offset);
                }
                // Remove processed words
                processedWords = dataWords.splice(0, nWordsReady);
                data.sigBytes -= nBytesReady;
            }
            // Return processed words
            return new WordArray(processedWords, nBytesReady);
        }
        /**
         * Creates a copy of this object.
         *
         * @return {Object} The clone.
         *
         * @example
         *
         *     var clone = bufferedBlockAlgorithm.clone();
         */
        clone() {
            const clone = super.clone.call(this);
            clone._data = this._data.clone();
            return clone;
        }
    }
    class Hasher extends BufferedBlockAlgorithm {
        constructor(cfg) {
            super();
            this.blockSize = 512 / 32;
            /**
             * Configuration options.
             */
            this.cfg = Object.assign(new Base(), cfg);
            // Set initial values
            this.reset();
        }
        /**
         * Creates a shortcut function to a hasher's object interface.
         *
         * @param {Hasher} SubHasher The hasher to create a helper for.
         *
         * @return {Function} The shortcut function.
         *
         * @static
         *
         * @example
         *
         *     var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
         */
        static _createHelper(SubHasher) {
            return (message, cfg) => new SubHasher(cfg).finalize(message);
        }
        /**
         * Creates a shortcut function to the HMAC's object interface.
         *
         * @param {Hasher} SubHasher The hasher to use in this HMAC helper.
         *
         * @return {Function} The shortcut function.
         *
         * @static
         *
         * @example
         *
         *     var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
         */
        static _createHmacHelper(SubHasher) {
            return (message, key) => new HMAC(SubHasher, key).finalize(message);
        }
        /**
         * Resets this hasher to its initial state.
         *
         * @example
         *
         *     hasher.reset();
         */
        reset() {
            // Reset data buffer
            super.reset.call(this);
            // Perform concrete-hasher logic
            this._doReset();
        }
        /**
         * Updates this hasher with a message.
         *
         * @param {WordArray|string} messageUpdate The message to append.
         *
         * @return {Hasher} This hasher.
         *
         * @example
         *
         *     hasher.update('message');
         *     hasher.update(wordArray);
         */
        update(messageUpdate) {
            // Append
            this._append(messageUpdate);
            // Update the hash
            this._process();
            // Chainable
            return this;
        }
        /**
         * Finalizes the hash computation.
         * Note that the finalize operation is effectively a destructive, read-once operation.
         *
         * @param {WordArray|string} messageUpdate (Optional) A final message update.
         *
         * @return {WordArray} The hash.
         *
         * @example
         *
         *     var hash = hasher.finalize();
         *     var hash = hasher.finalize('message');
         *     var hash = hasher.finalize(wordArray);
         */
        finalize(messageUpdate) {
            // Final message update
            if (messageUpdate) {
                this._append(messageUpdate);
            }
            // Perform concrete-hasher logic
            const hash = this._doFinalize();
            return hash;
        }
    }
    class HMAC extends Base {
        /**
         * Initializes a newly created HMAC.
         *
         * @param {Hasher} SubHasher The hash algorithm to use.
         * @param {WordArray|string} key The secret key.
         *
         * @example
         *
         *     var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
         */
        constructor(SubHasher, key) {
            super();
            const hasher = new SubHasher();
            this._hasher = hasher;
            // Convert string to WordArray, else assume WordArray already
            let _key = key;
            if (typeof _key === 'string') {
                _key = Utf8.parse(_key);
            }
            // Shortcuts
            const hasherBlockSize = hasher.blockSize;
            const hasherBlockSizeBytes = hasherBlockSize * 4;
            // Allow arbitrary length keys
            if (_key.sigBytes > hasherBlockSizeBytes) {
                _key = hasher.finalize(key);
            }
            // Clamp excess bits
            _key.clamp();
            // Clone key for inner and outer pads
            const oKey = _key.clone();
            this._oKey = oKey;
            const iKey = _key.clone();
            this._iKey = iKey;
            // Shortcuts
            const oKeyWords = oKey.words;
            const iKeyWords = iKey.words;
            // XOR keys with pad constants
            for (let i = 0; i < hasherBlockSize; i += 1) {
                oKeyWords[i] ^= 0x5c5c5c5c;
                iKeyWords[i] ^= 0x36363636;
            }
            oKey.sigBytes = hasherBlockSizeBytes;
            iKey.sigBytes = hasherBlockSizeBytes;
            // Set initial values
            this.reset();
        }
        /**
         * Resets this HMAC to its initial state.
         *
         * @example
         *
         *     hmacHasher.reset();
         */
        reset() {
            // Shortcut
            const hasher = this._hasher;
            // Reset
            hasher.reset();
            hasher.update(this._iKey);
        }
        /**
         * Updates this HMAC with a message.
         *
         * @param {WordArray|string} messageUpdate The message to append.
         *
         * @return {HMAC} This HMAC instance.
         *
         * @example
         *
         *     hmacHasher.update('message');
         *     hmacHasher.update(wordArray);
         */
        update(messageUpdate) {
            this._hasher.update(messageUpdate);
            // Chainable
            return this;
        }
        /**
         * Finalizes the HMAC computation.
         * Note that the finalize operation is effectively a destructive, read-once operation.
         *
         * @param {WordArray|string} messageUpdate (Optional) A final message update.
         *
         * @return {WordArray} The HMAC.
         *
         * @example
         *
         *     var hmac = hmacHasher.finalize();
         *     var hmac = hmacHasher.finalize('message');
         *     var hmac = hmacHasher.finalize(wordArray);
         */
        finalize(messageUpdate) {
            // Shortcut
            const hasher = this._hasher;
            // Compute HMAC
            const innerHash = hasher.finalize(messageUpdate);
            hasher.reset();
            const hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
            return hmac;
        }
    }
    // Initialization and round constants tables
    const H = [];
    const K = [];
    // Compute constants
    const isPrime = (n) => {
        const sqrtN = Math.sqrt(n);
        for (let factor = 2; factor <= sqrtN; factor += 1) {
            if (!(n % factor)) {
                return false;
            }
        }
        return true;
    };
    const getFractionalBits = n => ((n - (n | 0)) * 0x100000000) | 0;
    let n = 2;
    let nPrime = 0;
    while (nPrime < 64) {
        if (isPrime(n)) {
            if (nPrime < 8) {
                H[nPrime] = getFractionalBits(n ** (1 / 2));
            }
            K[nPrime] = getFractionalBits(n ** (1 / 3));
            nPrime += 1;
        }
        n += 1;
    }
    // Reusable object
    const W = [];
    class SHA256Algo extends Hasher {
        _doReset() {
            this._hash = new WordArray(H.slice(0));
        }
        _doProcessBlock(M, offset) {
            // Shortcut
            const _H = this._hash.words;
            // Working variables
            let a = _H[0];
            let b = _H[1];
            let c = _H[2];
            let d = _H[3];
            let e = _H[4];
            let f = _H[5];
            let g = _H[6];
            let h = _H[7];
            // Computation
            for (let i = 0; i < 64; i += 1) {
                if (i < 16) {
                    W[i] = M[offset + i] | 0;
                } else {
                    const gamma0x = W[i - 15];
                    const gamma0 = ((gamma0x << 25) | (gamma0x >>> 7))
                        ^ ((gamma0x << 14) | (gamma0x >>> 18))
                        ^ (gamma0x >>> 3);
                    const gamma1x = W[i - 2];
                    const gamma1 = ((gamma1x << 15) | (gamma1x >>> 17))
                        ^ ((gamma1x << 13) | (gamma1x >>> 19))
                        ^ (gamma1x >>> 10);
                    W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
                }
                const ch = (e & f) ^ (~e & g);
                const maj = (a & b) ^ (a & c) ^ (b & c);
                const sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
                const sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
                const t1 = h + sigma1 + ch + K[i] + W[i];
                const t2 = sigma0 + maj;
                h = g;
                g = f;
                f = e;
                e = (d + t1) | 0;
                d = c;
                c = b;
                b = a;
                a = (t1 + t2) | 0;
            }
            // Intermediate hash value
            _H[0] = (_H[0] + a) | 0;
            _H[1] = (_H[1] + b) | 0;
            _H[2] = (_H[2] + c) | 0;
            _H[3] = (_H[3] + d) | 0;
            _H[4] = (_H[4] + e) | 0;
            _H[5] = (_H[5] + f) | 0;
            _H[6] = (_H[6] + g) | 0;
            _H[7] = (_H[7] + h) | 0;
        }
        _doFinalize() {
            // Shortcuts
            const data = this._data;
            const dataWords = data.words;
            const nBitsTotal = this._nDataBytes * 8;
            const nBitsLeft = data.sigBytes * 8;
            // Add padding
            dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - (nBitsLeft % 32));
            dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
            dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
            data.sigBytes = dataWords.length * 4;
            // Hash final blocks
            this._process();
            // Return final computed hash
            return this._hash;
        }
        clone() {
            const clone = super.clone.call(this);
            clone._hash = this._hash.clone();
            return clone;
        }
    }
    CryptoJs.HmacSha256 = Hasher._createHmacHelper(SHA256Algo);
    CryptoJs.Base64 = {
        stringify: function (wordArray) {
            // Shortcuts
            const words = wordArray.words;
            const sigBytes = wordArray.sigBytes;
            const map = this._map;
            // Clamp excess bits
            wordArray.clamp();
            // Convert
            const base64Chars = [];
            for (let i = 0; i < sigBytes; i += 3) {
                const byte1 = (words[i >>> 2]       >>> (24 - (i % 4) * 8))       & 0xff;
                const byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
                const byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
                const triplet = (byte1 << 16) | (byte2 << 8) | byte3;
                for (let j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
                    base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
                }
            }
            // Add padding
            const paddingChar = map.charAt(64);
            if (paddingChar) {
                while (base64Chars.length % 4) {
                    base64Chars.push(paddingChar);
                }
            }
            return base64Chars.join('');
        },
        _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
    }
    return CryptoJs
}

以上就是CryptoJs常规密码加密demo的详细内容,更多关于CryptoJs加密的资料请关注脚本之家其它相关文章!

相关文章

最新评论