MCPcopy Create free account
hub / github.com/cryptii/cryptii / performEncode

Method performEncode

src/Encoder/HMAC.js:58–93  ·  view source on GitHub ↗

* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content

(content)

Source from the content-addressed store, hash-verified

56 * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
57 */
58 async performEncode (content) {
59 const message = content.getBytes()
60 const algorithm = this.getSettingValue('algorithm')
61 const blockSize = HashEncoder.getAlgorithmBlockSize(algorithm)
62
63 // Shorten keys longer than block size by sending it through the hash func
64 let key = this.getSettingValue('key')
65 if (key.length > blockSize) {
66 key = await this.createDigest(algorithm, key)
67 }
68
69 // Keys shorter than block size are padded to block size
70 const outerKey = new Uint8Array(blockSize)
71 outerKey.set(key, 0)
72
73 // Compose inner message and prepare outer key
74 const innerMessage = new Uint8Array(blockSize + message.length)
75 innerMessage.set(outerKey, 0)
76 innerMessage.set(message, blockSize)
77
78 for (let i = 0; i < blockSize; i++) {
79 innerMessage[i] ^= 0x36
80 outerKey[i] ^= 0x5C
81 }
82
83 // Calculate inner digest
84 const innerDigest = await this.createDigest(algorithm, innerMessage)
85
86 // Compose outer message
87 const outerMessage = new Uint8Array(blockSize + innerDigest.length)
88 outerMessage.set(outerKey, 0)
89 outerMessage.set(innerDigest, blockSize)
90
91 // Calculate hmac digest
92 return this.createDigest(algorithm, outerMessage)
93 }
94
95 /**
96 * Creates message digest using given algorithm.

Callers

nothing calls this directly

Calls 4

createDigestMethod · 0.95
getBytesMethod · 0.80
getSettingValueMethod · 0.80
getAlgorithmBlockSizeMethod · 0.80

Tested by

no test coverage detected