@bsv/sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.md +40 -0
- package/.github/ISSUE_TEMPLATE/discussion.md +24 -0
- package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +23 -0
- package/CHANGELOG.md +72 -0
- package/CONTRIBUTING.md +85 -0
- package/LICENSE.txt +28 -0
- package/README.md +87 -0
- package/ROADMAP.md +3 -0
- package/dist/cjs/mod.js +25 -0
- package/dist/cjs/mod.js.map +1 -0
- package/dist/cjs/package.json +42 -0
- package/dist/cjs/src/compat/BIP39.js +272 -0
- package/dist/cjs/src/compat/BIP39.js.map +1 -0
- package/dist/cjs/src/compat/BSM.js +77 -0
- package/dist/cjs/src/compat/BSM.js.map +1 -0
- package/dist/cjs/src/compat/ECIES.js +483 -0
- package/dist/cjs/src/compat/ECIES.js.map +1 -0
- package/dist/cjs/src/compat/HD.js +326 -0
- package/dist/cjs/src/compat/HD.js.map +1 -0
- package/dist/cjs/src/compat/Mnemonic.js +298 -0
- package/dist/cjs/src/compat/Mnemonic.js.map +1 -0
- package/dist/cjs/src/compat/bip-39-wordlist-en.js +2057 -0
- package/dist/cjs/src/compat/bip-39-wordlist-en.js.map +1 -0
- package/dist/cjs/src/compat/index.js +37 -0
- package/dist/cjs/src/compat/index.js.map +1 -0
- package/dist/cjs/src/messages/EncryptedMessage.js +69 -0
- package/dist/cjs/src/messages/EncryptedMessage.js.map +1 -0
- package/dist/cjs/src/messages/SignedMessage.js +85 -0
- package/dist/cjs/src/messages/SignedMessage.js.map +1 -0
- package/dist/cjs/src/messages/index.js +29 -0
- package/dist/cjs/src/messages/index.js.map +1 -0
- package/dist/cjs/src/primitives/AESGCM.js +384 -0
- package/dist/cjs/src/primitives/AESGCM.js.map +1 -0
- package/dist/cjs/src/primitives/BasePoint.js +19 -0
- package/dist/cjs/src/primitives/BasePoint.js.map +1 -0
- package/dist/cjs/src/primitives/BigNumber.js +4269 -0
- package/dist/cjs/src/primitives/BigNumber.js.map +1 -0
- package/dist/cjs/src/primitives/Curve.js +1126 -0
- package/dist/cjs/src/primitives/Curve.js.map +1 -0
- package/dist/cjs/src/primitives/DRBG.js +99 -0
- package/dist/cjs/src/primitives/DRBG.js.map +1 -0
- package/dist/cjs/src/primitives/ECDSA.js +169 -0
- package/dist/cjs/src/primitives/ECDSA.js.map +1 -0
- package/dist/cjs/src/primitives/Hash.js +1332 -0
- package/dist/cjs/src/primitives/Hash.js.map +1 -0
- package/dist/cjs/src/primitives/JacobianPoint.js +400 -0
- package/dist/cjs/src/primitives/JacobianPoint.js.map +1 -0
- package/dist/cjs/src/primitives/K256.js +111 -0
- package/dist/cjs/src/primitives/K256.js.map +1 -0
- package/dist/cjs/src/primitives/Mersenne.js +118 -0
- package/dist/cjs/src/primitives/Mersenne.js.map +1 -0
- package/dist/cjs/src/primitives/MontgomoryMethod.js +150 -0
- package/dist/cjs/src/primitives/MontgomoryMethod.js.map +1 -0
- package/dist/cjs/src/primitives/Point.js +819 -0
- package/dist/cjs/src/primitives/Point.js.map +1 -0
- package/dist/cjs/src/primitives/PrivateKey.js +190 -0
- package/dist/cjs/src/primitives/PrivateKey.js.map +1 -0
- package/dist/cjs/src/primitives/PublicKey.js +151 -0
- package/dist/cjs/src/primitives/PublicKey.js.map +1 -0
- package/dist/cjs/src/primitives/Random.js +57 -0
- package/dist/cjs/src/primitives/Random.js.map +1 -0
- package/dist/cjs/src/primitives/ReductionContext.js +490 -0
- package/dist/cjs/src/primitives/ReductionContext.js.map +1 -0
- package/dist/cjs/src/primitives/Signature.js +220 -0
- package/dist/cjs/src/primitives/Signature.js.map +1 -0
- package/dist/cjs/src/primitives/SymmetricKey.js +69 -0
- package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -0
- package/dist/cjs/src/primitives/TransactionSignature.js +172 -0
- package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -0
- package/dist/cjs/src/primitives/index.js +45 -0
- package/dist/cjs/src/primitives/index.js.map +1 -0
- package/dist/cjs/src/primitives/utils.js +615 -0
- package/dist/cjs/src/primitives/utils.js.map +1 -0
- package/dist/cjs/src/script/LockingScript.js +35 -0
- package/dist/cjs/src/script/LockingScript.js.map +1 -0
- package/dist/cjs/src/script/OP.js +208 -0
- package/dist/cjs/src/script/OP.js.map +1 -0
- package/dist/cjs/src/script/Script.js +429 -0
- package/dist/cjs/src/script/Script.js.map +1 -0
- package/dist/cjs/src/script/ScriptChunk.js +3 -0
- package/dist/cjs/src/script/ScriptChunk.js.map +1 -0
- package/dist/cjs/src/script/ScriptTemplate.js +3 -0
- package/dist/cjs/src/script/ScriptTemplate.js.map +1 -0
- package/dist/cjs/src/script/Spend.js +1252 -0
- package/dist/cjs/src/script/Spend.js.map +1 -0
- package/dist/cjs/src/script/UnlockingScript.js +35 -0
- package/dist/cjs/src/script/UnlockingScript.js.map +1 -0
- package/dist/cjs/src/script/index.js +32 -0
- package/dist/cjs/src/script/index.js.map +1 -0
- package/dist/cjs/src/script/templates/P2PKH.js +98 -0
- package/dist/cjs/src/script/templates/P2PKH.js.map +1 -0
- package/dist/cjs/src/script/templates/RPuzzle.js +125 -0
- package/dist/cjs/src/script/templates/RPuzzle.js.map +1 -0
- package/dist/cjs/src/script/templates/index.js +11 -0
- package/dist/cjs/src/script/templates/index.js.map +1 -0
- package/dist/cjs/src/transaction/Broadcaster.js +3 -0
- package/dist/cjs/src/transaction/Broadcaster.js.map +1 -0
- package/dist/cjs/src/transaction/ChainTracker.js +3 -0
- package/dist/cjs/src/transaction/ChainTracker.js.map +1 -0
- package/dist/cjs/src/transaction/FeeModel.js +3 -0
- package/dist/cjs/src/transaction/FeeModel.js.map +1 -0
- package/dist/cjs/src/transaction/MerklePath.js +239 -0
- package/dist/cjs/src/transaction/MerklePath.js.map +1 -0
- package/dist/cjs/src/transaction/Transaction.js +557 -0
- package/dist/cjs/src/transaction/Transaction.js.map +1 -0
- package/dist/cjs/src/transaction/TransactionInput.js +3 -0
- package/dist/cjs/src/transaction/TransactionInput.js.map +1 -0
- package/dist/cjs/src/transaction/TransactionOutput.js +3 -0
- package/dist/cjs/src/transaction/TransactionOutput.js.map +1 -0
- package/dist/cjs/src/transaction/broadcasters/ARC.js +101 -0
- package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -0
- package/dist/cjs/src/transaction/broadcasters/index.js +9 -0
- package/dist/cjs/src/transaction/broadcasters/index.js.map +1 -0
- package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js +69 -0
- package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -0
- package/dist/cjs/src/transaction/fee-models/index.js +9 -0
- package/dist/cjs/src/transaction/fee-models/index.js.map +1 -0
- package/dist/cjs/src/transaction/index.js +11 -0
- package/dist/cjs/src/transaction/index.js.map +1 -0
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
- package/dist/esm/mod.js +9 -0
- package/dist/esm/mod.js.map +1 -0
- package/dist/esm/src/compat/BIP39.js +272 -0
- package/dist/esm/src/compat/BIP39.js.map +1 -0
- package/dist/esm/src/compat/BSM.js +45 -0
- package/dist/esm/src/compat/BSM.js.map +1 -0
- package/dist/esm/src/compat/ECIES.js +454 -0
- package/dist/esm/src/compat/ECIES.js.map +1 -0
- package/dist/esm/src/compat/HD.js +304 -0
- package/dist/esm/src/compat/HD.js.map +1 -0
- package/dist/esm/src/compat/Mnemonic.js +272 -0
- package/dist/esm/src/compat/Mnemonic.js.map +1 -0
- package/dist/esm/src/compat/bip-39-wordlist-en.js +2054 -0
- package/dist/esm/src/compat/bip-39-wordlist-en.js.map +1 -0
- package/dist/esm/src/compat/index.js +5 -0
- package/dist/esm/src/compat/index.js.map +1 -0
- package/dist/esm/src/messages/EncryptedMessage.js +61 -0
- package/dist/esm/src/messages/EncryptedMessage.js.map +1 -0
- package/dist/esm/src/messages/SignedMessage.js +77 -0
- package/dist/esm/src/messages/SignedMessage.js.map +1 -0
- package/dist/esm/src/messages/index.js +3 -0
- package/dist/esm/src/messages/index.js.map +1 -0
- package/dist/esm/src/primitives/AESGCM.js +371 -0
- package/dist/esm/src/primitives/AESGCM.js.map +1 -0
- package/dist/esm/src/primitives/BasePoint.js +16 -0
- package/dist/esm/src/primitives/BasePoint.js.map +1 -0
- package/dist/esm/src/primitives/BigNumber.js +4304 -0
- package/dist/esm/src/primitives/BigNumber.js.map +1 -0
- package/dist/esm/src/primitives/Curve.js +1141 -0
- package/dist/esm/src/primitives/Curve.js.map +1 -0
- package/dist/esm/src/primitives/DRBG.js +98 -0
- package/dist/esm/src/primitives/DRBG.js.map +1 -0
- package/dist/esm/src/primitives/ECDSA.js +161 -0
- package/dist/esm/src/primitives/ECDSA.js.map +1 -0
- package/dist/esm/src/primitives/Hash.js +1336 -0
- package/dist/esm/src/primitives/Hash.js.map +1 -0
- package/dist/esm/src/primitives/JacobianPoint.js +398 -0
- package/dist/esm/src/primitives/JacobianPoint.js.map +1 -0
- package/dist/esm/src/primitives/K256.js +105 -0
- package/dist/esm/src/primitives/K256.js.map +1 -0
- package/dist/esm/src/primitives/Mersenne.js +117 -0
- package/dist/esm/src/primitives/Mersenne.js.map +1 -0
- package/dist/esm/src/primitives/MontgomoryMethod.js +149 -0
- package/dist/esm/src/primitives/MontgomoryMethod.js.map +1 -0
- package/dist/esm/src/primitives/Point.js +816 -0
- package/dist/esm/src/primitives/Point.js.map +1 -0
- package/dist/esm/src/primitives/PrivateKey.js +184 -0
- package/dist/esm/src/primitives/PrivateKey.js.map +1 -0
- package/dist/esm/src/primitives/PublicKey.js +145 -0
- package/dist/esm/src/primitives/PublicKey.js.map +1 -0
- package/dist/esm/src/primitives/Random.js +56 -0
- package/dist/esm/src/primitives/Random.js.map +1 -0
- package/dist/esm/src/primitives/ReductionContext.js +486 -0
- package/dist/esm/src/primitives/ReductionContext.js.map +1 -0
- package/dist/esm/src/primitives/Signature.js +223 -0
- package/dist/esm/src/primitives/Signature.js.map +1 -0
- package/dist/esm/src/primitives/SymmetricKey.js +63 -0
- package/dist/esm/src/primitives/SymmetricKey.js.map +1 -0
- package/dist/esm/src/primitives/TransactionSignature.js +144 -0
- package/dist/esm/src/primitives/TransactionSignature.js.map +1 -0
- package/dist/esm/src/primitives/index.js +9 -0
- package/dist/esm/src/primitives/index.js.map +1 -0
- package/dist/esm/src/primitives/utils.js +601 -0
- package/dist/esm/src/primitives/utils.js.map +1 -0
- package/dist/esm/src/script/LockingScript.js +29 -0
- package/dist/esm/src/script/LockingScript.js.map +1 -0
- package/dist/esm/src/script/OP.js +206 -0
- package/dist/esm/src/script/OP.js.map +1 -0
- package/dist/esm/src/script/Script.js +424 -0
- package/dist/esm/src/script/Script.js.map +1 -0
- package/dist/esm/src/script/ScriptChunk.js +2 -0
- package/dist/esm/src/script/ScriptChunk.js.map +1 -0
- package/dist/esm/src/script/ScriptTemplate.js +2 -0
- package/dist/esm/src/script/ScriptTemplate.js.map +1 -0
- package/dist/esm/src/script/Spend.js +1240 -0
- package/dist/esm/src/script/Spend.js.map +1 -0
- package/dist/esm/src/script/UnlockingScript.js +29 -0
- package/dist/esm/src/script/UnlockingScript.js.map +1 -0
- package/dist/esm/src/script/index.js +7 -0
- package/dist/esm/src/script/index.js.map +1 -0
- package/dist/esm/src/script/templates/P2PKH.js +92 -0
- package/dist/esm/src/script/templates/P2PKH.js.map +1 -0
- package/dist/esm/src/script/templates/RPuzzle.js +119 -0
- package/dist/esm/src/script/templates/RPuzzle.js.map +1 -0
- package/dist/esm/src/script/templates/index.js +3 -0
- package/dist/esm/src/script/templates/index.js.map +1 -0
- package/dist/esm/src/transaction/Broadcaster.js +2 -0
- package/dist/esm/src/transaction/Broadcaster.js.map +1 -0
- package/dist/esm/src/transaction/ChainTracker.js +2 -0
- package/dist/esm/src/transaction/ChainTracker.js.map +1 -0
- package/dist/esm/src/transaction/FeeModel.js +2 -0
- package/dist/esm/src/transaction/FeeModel.js.map +1 -0
- package/dist/esm/src/transaction/MerklePath.js +237 -0
- package/dist/esm/src/transaction/MerklePath.js.map +1 -0
- package/dist/esm/src/transaction/Transaction.js +557 -0
- package/dist/esm/src/transaction/Transaction.js.map +1 -0
- package/dist/esm/src/transaction/TransactionInput.js +2 -0
- package/dist/esm/src/transaction/TransactionInput.js.map +1 -0
- package/dist/esm/src/transaction/TransactionOutput.js +2 -0
- package/dist/esm/src/transaction/TransactionOutput.js.map +1 -0
- package/dist/esm/src/transaction/broadcasters/ARC.js +100 -0
- package/dist/esm/src/transaction/broadcasters/ARC.js.map +1 -0
- package/dist/esm/src/transaction/broadcasters/index.js +2 -0
- package/dist/esm/src/transaction/broadcasters/index.js.map +1 -0
- package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js +71 -0
- package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -0
- package/dist/esm/src/transaction/fee-models/index.js +2 -0
- package/dist/esm/src/transaction/fee-models/index.js.map +1 -0
- package/dist/esm/src/transaction/index.js +3 -0
- package/dist/esm/src/transaction/index.js.map +1 -0
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/types/mod.d.ts +9 -0
- package/dist/types/mod.d.ts.map +1 -0
- package/dist/types/src/compat/BIP39.d.ts +132 -0
- package/dist/types/src/compat/BIP39.d.ts.map +1 -0
- package/dist/types/src/compat/BSM.d.ts +28 -0
- package/dist/types/src/compat/BSM.d.ts.map +1 -0
- package/dist/types/src/compat/ECIES.d.ts +62 -0
- package/dist/types/src/compat/ECIES.d.ts.map +1 -0
- package/dist/types/src/compat/HD.d.ts +117 -0
- package/dist/types/src/compat/HD.d.ts.map +1 -0
- package/dist/types/src/compat/Mnemonic.d.ts +132 -0
- package/dist/types/src/compat/Mnemonic.d.ts.map +1 -0
- package/dist/types/src/compat/bip-39-wordlist-en.d.ts +5 -0
- package/dist/types/src/compat/bip-39-wordlist-en.d.ts.map +1 -0
- package/dist/types/src/compat/index.d.ts +5 -0
- package/dist/types/src/compat/index.d.ts.map +1 -0
- package/dist/types/src/messages/EncryptedMessage.d.ts +20 -0
- package/dist/types/src/messages/EncryptedMessage.d.ts.map +1 -0
- package/dist/types/src/messages/SignedMessage.d.ts +21 -0
- package/dist/types/src/messages/SignedMessage.d.ts.map +1 -0
- package/dist/types/src/messages/index.d.ts +3 -0
- package/dist/types/src/messages/index.d.ts.map +1 -0
- package/dist/types/src/primitives/AESGCM.d.ts +14 -0
- package/dist/types/src/primitives/AESGCM.d.ts.map +1 -0
- package/dist/types/src/primitives/BasePoint.d.ts +22 -0
- package/dist/types/src/primitives/BasePoint.d.ts.map +1 -0
- package/dist/types/src/primitives/BigNumber.d.ts +1895 -0
- package/dist/types/src/primitives/BigNumber.d.ts.map +1 -0
- package/dist/types/src/primitives/Curve.d.ts +55 -0
- package/dist/types/src/primitives/Curve.d.ts.map +1 -0
- package/dist/types/src/primitives/DRBG.d.ts +54 -0
- package/dist/types/src/primitives/DRBG.d.ts.map +1 -0
- package/dist/types/src/primitives/ECDSA.d.ts +39 -0
- package/dist/types/src/primitives/ECDSA.d.ts.map +1 -0
- package/dist/types/src/primitives/Hash.d.ts +411 -0
- package/dist/types/src/primitives/Hash.d.ts.map +1 -0
- package/dist/types/src/primitives/JacobianPoint.d.ts +164 -0
- package/dist/types/src/primitives/JacobianPoint.d.ts.map +1 -0
- package/dist/types/src/primitives/K256.d.ts +53 -0
- package/dist/types/src/primitives/K256.d.ts.map +1 -0
- package/dist/types/src/primitives/Mersenne.d.ts +72 -0
- package/dist/types/src/primitives/Mersenne.d.ts.map +1 -0
- package/dist/types/src/primitives/MontgomoryMethod.d.ts +96 -0
- package/dist/types/src/primitives/MontgomoryMethod.d.ts.map +1 -0
- package/dist/types/src/primitives/Point.d.ts +303 -0
- package/dist/types/src/primitives/Point.d.ts.map +1 -0
- package/dist/types/src/primitives/PrivateKey.d.ts +143 -0
- package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -0
- package/dist/types/src/primitives/PublicKey.d.ts +108 -0
- package/dist/types/src/primitives/PublicKey.d.ts.map +1 -0
- package/dist/types/src/primitives/Random.d.ts +14 -0
- package/dist/types/src/primitives/Random.d.ts.map +1 -0
- package/dist/types/src/primitives/ReductionContext.d.ts +308 -0
- package/dist/types/src/primitives/ReductionContext.d.ts.map +1 -0
- package/dist/types/src/primitives/Signature.d.ts +100 -0
- package/dist/types/src/primitives/Signature.d.ts.map +1 -0
- package/dist/types/src/primitives/SymmetricKey.d.ts +44 -0
- package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -0
- package/dist/types/src/primitives/TransactionSignature.d.ts +36 -0
- package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -0
- package/dist/types/src/primitives/index.d.ts +9 -0
- package/dist/types/src/primitives/index.d.ts.map +1 -0
- package/dist/types/src/primitives/utils.d.ts +118 -0
- package/dist/types/src/primitives/utils.d.ts.map +1 -0
- package/dist/types/src/script/LockingScript.d.ts +25 -0
- package/dist/types/src/script/LockingScript.d.ts.map +1 -0
- package/dist/types/src/script/OP.d.ts +193 -0
- package/dist/types/src/script/OP.d.ts.map +1 -0
- package/dist/types/src/script/Script.d.ts +148 -0
- package/dist/types/src/script/Script.d.ts.map +1 -0
- package/dist/types/src/script/ScriptChunk.d.ts +8 -0
- package/dist/types/src/script/ScriptChunk.d.ts.map +1 -0
- package/dist/types/src/script/ScriptTemplate.d.ts +33 -0
- package/dist/types/src/script/ScriptTemplate.d.ts.map +1 -0
- package/dist/types/src/script/Spend.d.ts +103 -0
- package/dist/types/src/script/Spend.d.ts.map +1 -0
- package/dist/types/src/script/UnlockingScript.d.ts +25 -0
- package/dist/types/src/script/UnlockingScript.d.ts.map +1 -0
- package/dist/types/src/script/index.d.ts +8 -0
- package/dist/types/src/script/index.d.ts.map +1 -0
- package/dist/types/src/script/templates/P2PKH.d.ts +37 -0
- package/dist/types/src/script/templates/P2PKH.d.ts.map +1 -0
- package/dist/types/src/script/templates/RPuzzle.d.ts +47 -0
- package/dist/types/src/script/templates/RPuzzle.d.ts.map +1 -0
- package/dist/types/src/script/templates/index.d.ts +3 -0
- package/dist/types/src/script/templates/index.d.ts.map +1 -0
- package/dist/types/src/transaction/Broadcaster.d.ts +39 -0
- package/dist/types/src/transaction/Broadcaster.d.ts.map +1 -0
- package/dist/types/src/transaction/ChainTracker.d.ts +23 -0
- package/dist/types/src/transaction/ChainTracker.d.ts.map +1 -0
- package/dist/types/src/transaction/FeeModel.d.ts +12 -0
- package/dist/types/src/transaction/FeeModel.d.ts.map +1 -0
- package/dist/types/src/transaction/MerklePath.d.ts +91 -0
- package/dist/types/src/transaction/MerklePath.d.ts.map +1 -0
- package/dist/types/src/transaction/Transaction.d.ts +181 -0
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -0
- package/dist/types/src/transaction/TransactionInput.d.ts +63 -0
- package/dist/types/src/transaction/TransactionInput.d.ts.map +1 -0
- package/dist/types/src/transaction/TransactionOutput.d.ts +36 -0
- package/dist/types/src/transaction/TransactionOutput.d.ts.map +1 -0
- package/dist/types/src/transaction/broadcasters/ARC.d.ts +28 -0
- package/dist/types/src/transaction/broadcasters/ARC.d.ts.map +1 -0
- package/dist/types/src/transaction/broadcasters/index.d.ts +2 -0
- package/dist/types/src/transaction/broadcasters/index.d.ts.map +1 -0
- package/dist/types/src/transaction/fee-models/SatoshisPerKilobyte.d.ts +26 -0
- package/dist/types/src/transaction/fee-models/SatoshisPerKilobyte.d.ts.map +1 -0
- package/dist/types/src/transaction/fee-models/index.d.ts +2 -0
- package/dist/types/src/transaction/fee-models/index.d.ts.map +1 -0
- package/dist/types/src/transaction/index.d.ts +7 -0
- package/dist/types/src/transaction/index.d.ts.map +1 -0
- package/dist/types/tsconfig.types.tsbuildinfo +1 -0
- package/docs/README.md +9 -0
- package/docs/compat.md +2856 -0
- package/docs/getting-started/COMMONJS.md +94 -0
- package/docs/getting-started/REACT-TS.md +131 -0
- package/docs/getting-started/TS-NODE.md +106 -0
- package/docs/getting-started/VUE.md +103 -0
- package/docs/messages.md +146 -0
- package/docs/primitives.md +7440 -0
- package/docs/script.md +766 -0
- package/docs/transaction.md +741 -0
- package/jest.config.js +6 -0
- package/mod.ts +8 -0
- package/package.json +137 -0
- package/src/compat/BSM.ts +51 -0
- package/src/compat/ECIES.ts +557 -0
- package/src/compat/HD.ts +348 -0
- package/src/compat/Mnemonic.ts +295 -0
- package/src/compat/__tests/BSM.test.ts +38 -0
- package/src/compat/__tests/ECIES.test.ts +90 -0
- package/src/compat/__tests/HD.test.ts +405 -0
- package/src/compat/__tests/Mnemonic.test.ts +177 -0
- package/src/compat/__tests/Mnemonic.vectors.ts +172 -0
- package/src/compat/bip-39-wordlist-en.ts +2053 -0
- package/src/compat/index.ts +4 -0
- package/src/messages/EncryptedMessage.ts +70 -0
- package/src/messages/SignedMessage.ts +87 -0
- package/src/messages/__tests/EncryptedMessage.test.ts +36 -0
- package/src/messages/__tests/SignedMessage.test.ts +53 -0
- package/src/messages/index.ts +2 -0
- package/src/primitives/AESGCM.ts +479 -0
- package/src/primitives/BasePoint.ts +21 -0
- package/src/primitives/BigNumber.ts +4619 -0
- package/src/primitives/Curve.ts +1163 -0
- package/src/primitives/DRBG.ts +102 -0
- package/src/primitives/ECDSA.ts +164 -0
- package/src/primitives/Hash.ts +1420 -0
- package/src/primitives/JacobianPoint.ts +410 -0
- package/src/primitives/K256.ts +116 -0
- package/src/primitives/Mersenne.ts +123 -0
- package/src/primitives/MontgomoryMethod.ts +160 -0
- package/src/primitives/Point.ts +852 -0
- package/src/primitives/PrivateKey.ts +195 -0
- package/src/primitives/PublicKey.ts +154 -0
- package/src/primitives/Random.ts +55 -0
- package/src/primitives/ReductionContext.ts +528 -0
- package/src/primitives/Signature.ts +235 -0
- package/src/primitives/SymmetricKey.ts +75 -0
- package/src/primitives/TransactionSignature.ts +189 -0
- package/src/primitives/__tests/AESGCM.test.ts +338 -0
- package/src/primitives/__tests/BRC42.private.vectors.ts +33 -0
- package/src/primitives/__tests/BRC42.public.vectors.ts +33 -0
- package/src/primitives/__tests/BigNumber.arithmatic.test.ts +572 -0
- package/src/primitives/__tests/BigNumber.binary.test.ts +203 -0
- package/src/primitives/__tests/BigNumber.constructor.test.ts +176 -0
- package/src/primitives/__tests/BigNumber.dhGroup.test.ts +18 -0
- package/src/primitives/__tests/BigNumber.fixtures.ts +264 -0
- package/src/primitives/__tests/BigNumber.serializers.test.ts +157 -0
- package/src/primitives/__tests/BigNumber.utils.test.ts +347 -0
- package/src/primitives/__tests/Curve.unit.test.ts +192 -0
- package/src/primitives/__tests/DRBG.test.ts +18 -0
- package/src/primitives/__tests/DRBG.vectors.ts +167 -0
- package/src/primitives/__tests/ECDH.test.ts +31 -0
- package/src/primitives/__tests/ECDSA.test.ts +58 -0
- package/src/primitives/__tests/HMAC.test.ts +59 -0
- package/src/primitives/__tests/Hash.test.ts +121 -0
- package/src/primitives/__tests/PBKDF2.vectors.ts +119 -0
- package/src/primitives/__tests/PrivateKey.test.ts +17 -0
- package/src/primitives/__tests/PublicKey.test.ts +66 -0
- package/src/primitives/__tests/Random.test.ts +14 -0
- package/src/primitives/__tests/Reader.test.ts +296 -0
- package/src/primitives/__tests/ReductionContext.test.ts +279 -0
- package/src/primitives/__tests/SymmetricKey.test.ts +58 -0
- package/src/primitives/__tests/SymmetricKey.vectors.ts +40 -0
- package/src/primitives/__tests/Writer.test.ts +198 -0
- package/src/primitives/__tests/sighash.vectors.ts +3503 -0
- package/src/primitives/__tests/utils.test.ts +108 -0
- package/src/primitives/index.ts +8 -0
- package/src/primitives/utils.ts +665 -0
- package/src/script/LockingScript.ts +30 -0
- package/src/script/OP.ts +219 -0
- package/src/script/Script.ts +426 -0
- package/src/script/ScriptChunk.ts +7 -0
- package/src/script/ScriptTemplate.ts +36 -0
- package/src/script/Spend.ts +1379 -0
- package/src/script/UnlockingScript.ts +30 -0
- package/src/script/__tests/Script.test.ts +369 -0
- package/src/script/__tests/Spend.test.ts +248 -0
- package/src/script/__tests/script.invalid.vectors.ts +925 -0
- package/src/script/__tests/script.valid.vectors.ts +1120 -0
- package/src/script/__tests/scriptFromVector.ts +42 -0
- package/src/script/__tests/spend.valid.vectors.ts +2288 -0
- package/src/script/index.ts +7 -0
- package/src/script/templates/P2PKH.ts +109 -0
- package/src/script/templates/RPuzzle.ts +140 -0
- package/src/script/templates/index.ts +2 -0
- package/src/transaction/Broadcaster.ts +42 -0
- package/src/transaction/ChainTracker.ts +22 -0
- package/src/transaction/FeeModel.ts +13 -0
- package/src/transaction/MerklePath.ts +259 -0
- package/src/transaction/Transaction.ts +602 -0
- package/src/transaction/TransactionInput.ts +63 -0
- package/src/transaction/TransactionOutput.ts +37 -0
- package/src/transaction/__tests/MerklePath.test.ts +181 -0
- package/src/transaction/__tests/Transaction.test.ts +413 -0
- package/src/transaction/__tests/bigtx.vectors.ts +4 -0
- package/src/transaction/__tests/bump.invalid.vectors.ts +8 -0
- package/src/transaction/__tests/bump.valid.vectors.ts +4 -0
- package/src/transaction/__tests/tx.invalid.vectors.ts +281 -0
- package/src/transaction/__tests/tx.valid.vectors.ts +364 -0
- package/src/transaction/broadcasters/ARC.ts +106 -0
- package/src/transaction/broadcasters/__tests/ARC.test.ts +115 -0
- package/src/transaction/broadcasters/index.ts +1 -0
- package/src/transaction/fee-models/SatoshisPerKilobyte.ts +71 -0
- package/src/transaction/fee-models/index.ts +1 -0
- package/src/transaction/index.ts +6 -0
- package/ts2md.json +5 -0
- package/tsconfig.base.json +26 -0
- package/tsconfig.cjs.json +11 -0
- package/tsconfig.eslint.json +12 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +17 -0
- package/tsconfig.types.json +11 -0
|
@@ -0,0 +1,1332 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pbkdf2 = exports.sha512hmac = exports.sha256hmac = exports.hash160 = exports.hash256 = exports.sha512 = exports.sha256 = exports.sha1 = exports.ripemd160 = exports.SHA512HMAC = exports.SHA256HMAC = exports.SHA512 = exports.SHA1 = exports.SHA256 = exports.RIPEMD160 = exports.toArray = void 0;
|
|
4
|
+
const assert = (expression, message = 'Hash assertion failed') => {
|
|
5
|
+
if (!expression) {
|
|
6
|
+
throw new Error(message);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The BaseHash class is an abstract base class for cryptographic hash functions.
|
|
11
|
+
* It provides a common structure and functionality for hash function classes.
|
|
12
|
+
*
|
|
13
|
+
* @class BaseHash
|
|
14
|
+
*
|
|
15
|
+
* @property pending - Stores partially processed message segments.
|
|
16
|
+
* @property pendingTotal - The total number of characters that are being stored in `pending`
|
|
17
|
+
* @property blockSize - The size of each block to processed.
|
|
18
|
+
* @property outSize - The size of the final hash output.
|
|
19
|
+
* @property endian - The endianness used during processing, can either be 'big' or 'little'.
|
|
20
|
+
* @property _delta8 - The block size divided by 8, useful in various computations.
|
|
21
|
+
* @property _delta32 - The block size divided by 32, useful in various computations.
|
|
22
|
+
* @property padLength - The length of padding to be added to finalize the computation.
|
|
23
|
+
* @property hmacStrength - The HMAC strength value.
|
|
24
|
+
*
|
|
25
|
+
* @param blockSize - The size of the block to be hashed.
|
|
26
|
+
* @param outSize - The size of the resulting hash.
|
|
27
|
+
* @param hmacStrength - The strength of the HMAC.
|
|
28
|
+
* @param padLength - The length of the padding to be added.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* Sub-classes would extend this base BaseHash class like:
|
|
32
|
+
* class RIPEMD160 extends BaseHash {
|
|
33
|
+
* constructor () {
|
|
34
|
+
* super(512, 160, 192, 64);
|
|
35
|
+
* // ...
|
|
36
|
+
* }
|
|
37
|
+
* // ...
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
class BaseHash {
|
|
41
|
+
constructor(blockSize, outSize, hmacStrength, padLength) {
|
|
42
|
+
this.pending = null;
|
|
43
|
+
this.pendingTotal = 0;
|
|
44
|
+
this.blockSize = blockSize;
|
|
45
|
+
this.outSize = outSize;
|
|
46
|
+
this.hmacStrength = hmacStrength;
|
|
47
|
+
this.padLength = padLength / 8;
|
|
48
|
+
this.endian = 'big';
|
|
49
|
+
this._delta8 = this.blockSize / 8;
|
|
50
|
+
this._delta32 = this.blockSize / 32;
|
|
51
|
+
}
|
|
52
|
+
_update(msg, start) {
|
|
53
|
+
throw new Error('Not implemented');
|
|
54
|
+
}
|
|
55
|
+
_digest(enc) {
|
|
56
|
+
throw new Error('Not implemented');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Converts the input message into an array, pads it, and joins into 32bit blocks.
|
|
60
|
+
* If there is enough data, it tries updating the hash computation.
|
|
61
|
+
*
|
|
62
|
+
* @method update
|
|
63
|
+
* @param msg - The message segment to include in the hashing computation.
|
|
64
|
+
* @param enc - The encoding of the message. If 'hex', the string will be treated as such, 'utf8' otherwise.
|
|
65
|
+
*
|
|
66
|
+
* @returns Returns the instance of the object for chaining.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* sha256.update('Hello World', 'utf8');
|
|
70
|
+
*/
|
|
71
|
+
update(msg, enc) {
|
|
72
|
+
// Convert message to array, pad it, and join into 32bit blocks
|
|
73
|
+
msg = toArray(msg, enc);
|
|
74
|
+
if (this.pending == null) {
|
|
75
|
+
this.pending = msg;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.pending = this.pending.concat(msg);
|
|
79
|
+
}
|
|
80
|
+
this.pendingTotal += msg.length;
|
|
81
|
+
// Enough data, try updating
|
|
82
|
+
if (this.pending.length >= this._delta8) {
|
|
83
|
+
msg = this.pending;
|
|
84
|
+
// Process pending data in blocks
|
|
85
|
+
const r = msg.length % this._delta8;
|
|
86
|
+
this.pending = msg.slice(msg.length - r, msg.length);
|
|
87
|
+
if (this.pending.length === 0) {
|
|
88
|
+
this.pending = null;
|
|
89
|
+
}
|
|
90
|
+
msg = join32(msg, 0, msg.length - r, this.endian);
|
|
91
|
+
for (let i = 0; i < msg.length; i += this._delta32) {
|
|
92
|
+
this._update(msg, i);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Finalizes the hash computation and returns the hash value/result.
|
|
99
|
+
*
|
|
100
|
+
* @method digest
|
|
101
|
+
* @param enc - The encoding of the final hash. If 'hex' then a hex string will be provided, otherwise an array of numbers.
|
|
102
|
+
*
|
|
103
|
+
* @returns Returns the final hash value.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* const hash = sha256.digest('hex');
|
|
107
|
+
*/
|
|
108
|
+
digest(enc) {
|
|
109
|
+
this.update(this._pad());
|
|
110
|
+
assert(this.pending === null);
|
|
111
|
+
return this._digest(enc);
|
|
112
|
+
}
|
|
113
|
+
;
|
|
114
|
+
/**
|
|
115
|
+
* [Private Method] Used internally to prepare the padding for the final stage of the hash computation.
|
|
116
|
+
*
|
|
117
|
+
* @method _pad
|
|
118
|
+
* @private
|
|
119
|
+
*
|
|
120
|
+
* @returns Returns an array denoting the padding.
|
|
121
|
+
*/
|
|
122
|
+
_pad() {
|
|
123
|
+
let len = this.pendingTotal;
|
|
124
|
+
const bytes = this._delta8;
|
|
125
|
+
const k = bytes - ((len + this.padLength) % bytes);
|
|
126
|
+
const res = new Array(k + this.padLength);
|
|
127
|
+
res[0] = 0x80;
|
|
128
|
+
let i;
|
|
129
|
+
for (i = 1; i < k; i++) {
|
|
130
|
+
res[i] = 0;
|
|
131
|
+
}
|
|
132
|
+
// Append length
|
|
133
|
+
len <<= 3;
|
|
134
|
+
let t;
|
|
135
|
+
if (this.endian === 'big') {
|
|
136
|
+
for (t = 8; t < this.padLength; t++) {
|
|
137
|
+
res[i++] = 0;
|
|
138
|
+
}
|
|
139
|
+
res[i++] = 0;
|
|
140
|
+
res[i++] = 0;
|
|
141
|
+
res[i++] = 0;
|
|
142
|
+
res[i++] = 0;
|
|
143
|
+
res[i++] = (len >>> 24) & 0xff;
|
|
144
|
+
res[i++] = (len >>> 16) & 0xff;
|
|
145
|
+
res[i++] = (len >>> 8) & 0xff;
|
|
146
|
+
res[i++] = len & 0xff;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
res[i++] = len & 0xff;
|
|
150
|
+
res[i++] = (len >>> 8) & 0xff;
|
|
151
|
+
res[i++] = (len >>> 16) & 0xff;
|
|
152
|
+
res[i++] = (len >>> 24) & 0xff;
|
|
153
|
+
res[i++] = 0;
|
|
154
|
+
res[i++] = 0;
|
|
155
|
+
res[i++] = 0;
|
|
156
|
+
res[i++] = 0;
|
|
157
|
+
for (t = 8; t < this.padLength; t++) {
|
|
158
|
+
res[i++] = 0;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return res;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function isSurrogatePair(msg, i) {
|
|
165
|
+
if ((msg.charCodeAt(i) & 0xFC00) !== 0xD800) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
if (i < 0 || i + 1 >= msg.length) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return (msg.charCodeAt(i + 1) & 0xFC00) === 0xDC00;
|
|
172
|
+
}
|
|
173
|
+
function toArray(msg, enc) {
|
|
174
|
+
if (Array.isArray(msg)) {
|
|
175
|
+
return msg.slice();
|
|
176
|
+
}
|
|
177
|
+
if (!msg) {
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
const res = [];
|
|
181
|
+
if (typeof msg === 'string') {
|
|
182
|
+
if (enc !== 'hex') {
|
|
183
|
+
// Inspired by stringToUtf8ByteArray() in closure-library by Google
|
|
184
|
+
// https://github.com/google/closure-library/blob/8598d87242af59aac233270742c8984e2b2bdbe0/closure/goog/crypt/crypt.js#L117-L143
|
|
185
|
+
// Apache License 2.0
|
|
186
|
+
// https://github.com/google/closure-library/blob/master/LICENSE
|
|
187
|
+
let p = 0;
|
|
188
|
+
for (let i = 0; i < msg.length; i++) {
|
|
189
|
+
let c = msg.charCodeAt(i);
|
|
190
|
+
if (c < 128) {
|
|
191
|
+
res[p++] = c;
|
|
192
|
+
}
|
|
193
|
+
else if (c < 2048) {
|
|
194
|
+
res[p++] = (c >> 6) | 192;
|
|
195
|
+
res[p++] = (c & 63) | 128;
|
|
196
|
+
}
|
|
197
|
+
else if (isSurrogatePair(msg, i)) {
|
|
198
|
+
c = 0x10000 + ((c & 0x03FF) << 10) + (msg.charCodeAt(++i) & 0x03FF);
|
|
199
|
+
res[p++] = (c >> 18) | 240;
|
|
200
|
+
res[p++] = ((c >> 12) & 63) | 128;
|
|
201
|
+
res[p++] = ((c >> 6) & 63) | 128;
|
|
202
|
+
res[p++] = (c & 63) | 128;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
res[p++] = (c >> 12) | 224;
|
|
206
|
+
res[p++] = ((c >> 6) & 63) | 128;
|
|
207
|
+
res[p++] = (c & 63) | 128;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
msg = msg.replace(/[^a-z0-9]+/ig, '');
|
|
213
|
+
if (msg.length % 2 !== 0) {
|
|
214
|
+
msg = '0' + msg;
|
|
215
|
+
}
|
|
216
|
+
for (let i = 0; i < msg.length; i += 2) {
|
|
217
|
+
res.push(parseInt(msg[i] + msg[i + 1], 16));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
msg = msg;
|
|
223
|
+
for (let i = 0; i < msg.length; i++) {
|
|
224
|
+
res[i] = msg[i] | 0;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return res;
|
|
228
|
+
}
|
|
229
|
+
exports.toArray = toArray;
|
|
230
|
+
function htonl(w) {
|
|
231
|
+
const res = (w >>> 24) |
|
|
232
|
+
((w >>> 8) & 0xff00) |
|
|
233
|
+
((w << 8) & 0xff0000) |
|
|
234
|
+
((w & 0xff) << 24);
|
|
235
|
+
return res >>> 0;
|
|
236
|
+
}
|
|
237
|
+
function toHex32(msg, endian) {
|
|
238
|
+
let res = '';
|
|
239
|
+
for (let i = 0; i < msg.length; i++) {
|
|
240
|
+
let w = msg[i];
|
|
241
|
+
if (endian === 'little') {
|
|
242
|
+
w = htonl(w);
|
|
243
|
+
}
|
|
244
|
+
res += zero8(w.toString(16));
|
|
245
|
+
}
|
|
246
|
+
return res;
|
|
247
|
+
}
|
|
248
|
+
function zero8(word) {
|
|
249
|
+
if (word.length === 7) {
|
|
250
|
+
return '0' + word;
|
|
251
|
+
}
|
|
252
|
+
else if (word.length === 6) {
|
|
253
|
+
return '00' + word;
|
|
254
|
+
}
|
|
255
|
+
else if (word.length === 5) {
|
|
256
|
+
return '000' + word;
|
|
257
|
+
}
|
|
258
|
+
else if (word.length === 4) {
|
|
259
|
+
return '0000' + word;
|
|
260
|
+
}
|
|
261
|
+
else if (word.length === 3) {
|
|
262
|
+
return '00000' + word;
|
|
263
|
+
}
|
|
264
|
+
else if (word.length === 2) {
|
|
265
|
+
return '000000' + word;
|
|
266
|
+
}
|
|
267
|
+
else if (word.length === 1) {
|
|
268
|
+
return '0000000' + word;
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
return word;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function join32(msg, start, end, endian) {
|
|
275
|
+
const len = end - start;
|
|
276
|
+
assert(len % 4 === 0);
|
|
277
|
+
const res = new Array(len / 4);
|
|
278
|
+
for (let i = 0, k = start; i < res.length; i++, k += 4) {
|
|
279
|
+
let w;
|
|
280
|
+
if (endian === 'big') {
|
|
281
|
+
w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3];
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
w = (msg[k + 3] << 24) | (msg[k + 2] << 16) | (msg[k + 1] << 8) | msg[k];
|
|
285
|
+
}
|
|
286
|
+
res[i] = w >>> 0;
|
|
287
|
+
}
|
|
288
|
+
return res;
|
|
289
|
+
}
|
|
290
|
+
function split32(msg, endian) {
|
|
291
|
+
const res = new Array(msg.length * 4);
|
|
292
|
+
for (let i = 0, k = 0; i < msg.length; i++, k += 4) {
|
|
293
|
+
const m = msg[i];
|
|
294
|
+
if (endian === 'big') {
|
|
295
|
+
res[k] = m >>> 24;
|
|
296
|
+
res[k + 1] = (m >>> 16) & 0xff;
|
|
297
|
+
res[k + 2] = (m >>> 8) & 0xff;
|
|
298
|
+
res[k + 3] = m & 0xff;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
res[k + 3] = m >>> 24;
|
|
302
|
+
res[k + 2] = (m >>> 16) & 0xff;
|
|
303
|
+
res[k + 1] = (m >>> 8) & 0xff;
|
|
304
|
+
res[k] = m & 0xff;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return res;
|
|
308
|
+
}
|
|
309
|
+
function rotr32(w, b) {
|
|
310
|
+
return (w >>> b) | (w << (32 - b));
|
|
311
|
+
}
|
|
312
|
+
function rotl32(w, b) {
|
|
313
|
+
return (w << b) | (w >>> (32 - b));
|
|
314
|
+
}
|
|
315
|
+
function sum32(a, b) {
|
|
316
|
+
return (a + b) >>> 0;
|
|
317
|
+
}
|
|
318
|
+
function SUM32_3(a, b, c) {
|
|
319
|
+
return (a + b + c) >>> 0;
|
|
320
|
+
}
|
|
321
|
+
function SUM32_4(a, b, c, d) {
|
|
322
|
+
return (a + b + c + d) >>> 0;
|
|
323
|
+
}
|
|
324
|
+
function SUM32_5(a, b, c, d, e) {
|
|
325
|
+
return (a + b + c + d + e) >>> 0;
|
|
326
|
+
}
|
|
327
|
+
function FT_1(s, x, y, z) {
|
|
328
|
+
if (s === 0) {
|
|
329
|
+
return ch32(x, y, z);
|
|
330
|
+
}
|
|
331
|
+
if (s === 1 || s === 3) {
|
|
332
|
+
return p32(x, y, z);
|
|
333
|
+
}
|
|
334
|
+
if (s === 2) {
|
|
335
|
+
return maj32(x, y, z);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function ch32(x, y, z) {
|
|
339
|
+
return (x & y) ^ ((~x) & z);
|
|
340
|
+
}
|
|
341
|
+
function maj32(x, y, z) {
|
|
342
|
+
return (x & y) ^ (x & z) ^ (y & z);
|
|
343
|
+
}
|
|
344
|
+
function p32(x, y, z) {
|
|
345
|
+
return x ^ y ^ z;
|
|
346
|
+
}
|
|
347
|
+
function S0_256(x) {
|
|
348
|
+
return rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22);
|
|
349
|
+
}
|
|
350
|
+
function S1_256(x) {
|
|
351
|
+
return rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25);
|
|
352
|
+
}
|
|
353
|
+
function G0_256(x) {
|
|
354
|
+
return rotr32(x, 7) ^ rotr32(x, 18) ^ (x >>> 3);
|
|
355
|
+
}
|
|
356
|
+
function G1_256(x) {
|
|
357
|
+
return rotr32(x, 17) ^ rotr32(x, 19) ^ (x >>> 10);
|
|
358
|
+
}
|
|
359
|
+
const r = [
|
|
360
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
361
|
+
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
|
|
362
|
+
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
|
|
363
|
+
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
|
|
364
|
+
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
|
|
365
|
+
];
|
|
366
|
+
const rh = [
|
|
367
|
+
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
|
|
368
|
+
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
|
|
369
|
+
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
|
|
370
|
+
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
|
|
371
|
+
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
|
|
372
|
+
];
|
|
373
|
+
const s = [
|
|
374
|
+
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
|
|
375
|
+
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
|
|
376
|
+
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
|
|
377
|
+
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
|
|
378
|
+
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
|
|
379
|
+
];
|
|
380
|
+
const sh = [
|
|
381
|
+
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
|
|
382
|
+
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
|
|
383
|
+
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
|
|
384
|
+
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
|
|
385
|
+
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
|
|
386
|
+
];
|
|
387
|
+
function f(j, x, y, z) {
|
|
388
|
+
if (j <= 15) {
|
|
389
|
+
return x ^ y ^ z;
|
|
390
|
+
}
|
|
391
|
+
else if (j <= 31) {
|
|
392
|
+
return (x & y) | ((~x) & z);
|
|
393
|
+
}
|
|
394
|
+
else if (j <= 47) {
|
|
395
|
+
return (x | (~y)) ^ z;
|
|
396
|
+
}
|
|
397
|
+
else if (j <= 63) {
|
|
398
|
+
return (x & z) | (y & (~z));
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
return x ^ (y | (~z));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
function K(j) {
|
|
405
|
+
if (j <= 15) {
|
|
406
|
+
return 0x00000000;
|
|
407
|
+
}
|
|
408
|
+
else if (j <= 31) {
|
|
409
|
+
return 0x5a827999;
|
|
410
|
+
}
|
|
411
|
+
else if (j <= 47) {
|
|
412
|
+
return 0x6ed9eba1;
|
|
413
|
+
}
|
|
414
|
+
else if (j <= 63) {
|
|
415
|
+
return 0x8f1bbcdc;
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
return 0xa953fd4e;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
function Kh(j) {
|
|
422
|
+
if (j <= 15) {
|
|
423
|
+
return 0x50a28be6;
|
|
424
|
+
}
|
|
425
|
+
else if (j <= 31) {
|
|
426
|
+
return 0x5c4dd124;
|
|
427
|
+
}
|
|
428
|
+
else if (j <= 47) {
|
|
429
|
+
return 0x6d703ef3;
|
|
430
|
+
}
|
|
431
|
+
else if (j <= 63) {
|
|
432
|
+
return 0x7a6d76e9;
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
return 0x00000000;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function sum64(buf, pos, ah, al) {
|
|
439
|
+
const bh = buf[pos];
|
|
440
|
+
const bl = buf[pos + 1];
|
|
441
|
+
const lo = (al + bl) >>> 0;
|
|
442
|
+
const hi = (lo < al ? 1 : 0) + ah + bh;
|
|
443
|
+
buf[pos] = hi >>> 0;
|
|
444
|
+
buf[pos + 1] = lo;
|
|
445
|
+
}
|
|
446
|
+
function sum64_hi(ah, al, bh, bl) {
|
|
447
|
+
const lo = (al + bl) >>> 0;
|
|
448
|
+
const hi = (lo < al ? 1 : 0) + ah + bh;
|
|
449
|
+
return hi >>> 0;
|
|
450
|
+
}
|
|
451
|
+
function sum64_lo(ah, al, bh, bl) {
|
|
452
|
+
const lo = al + bl;
|
|
453
|
+
return lo >>> 0;
|
|
454
|
+
}
|
|
455
|
+
function sum64_4_hi(ah, al, bh, bl, ch, cl, dh, dl) {
|
|
456
|
+
let carry = 0;
|
|
457
|
+
let lo = al;
|
|
458
|
+
lo = (lo + bl) >>> 0;
|
|
459
|
+
carry += lo < al ? 1 : 0;
|
|
460
|
+
lo = (lo + cl) >>> 0;
|
|
461
|
+
carry += lo < cl ? 1 : 0;
|
|
462
|
+
lo = (lo + dl) >>> 0;
|
|
463
|
+
carry += lo < dl ? 1 : 0;
|
|
464
|
+
const hi = ah + bh + ch + dh + carry;
|
|
465
|
+
return hi >>> 0;
|
|
466
|
+
}
|
|
467
|
+
function sum64_4_lo(ah, al, bh, bl, ch, cl, dh, dl) {
|
|
468
|
+
const lo = al + bl + cl + dl;
|
|
469
|
+
return lo >>> 0;
|
|
470
|
+
}
|
|
471
|
+
function sum64_5_hi(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
|
|
472
|
+
let carry = 0;
|
|
473
|
+
let lo = al;
|
|
474
|
+
lo = (lo + bl) >>> 0;
|
|
475
|
+
carry += lo < al ? 1 : 0;
|
|
476
|
+
lo = (lo + cl) >>> 0;
|
|
477
|
+
carry += lo < cl ? 1 : 0;
|
|
478
|
+
lo = (lo + dl) >>> 0;
|
|
479
|
+
carry += lo < dl ? 1 : 0;
|
|
480
|
+
lo = (lo + el) >>> 0;
|
|
481
|
+
carry += lo < el ? 1 : 0;
|
|
482
|
+
const hi = ah + bh + ch + dh + eh + carry;
|
|
483
|
+
return hi >>> 0;
|
|
484
|
+
}
|
|
485
|
+
function sum64_5_lo(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
|
|
486
|
+
const lo = al + bl + cl + dl + el;
|
|
487
|
+
return lo >>> 0;
|
|
488
|
+
}
|
|
489
|
+
function rotr64_hi(ah, al, num) {
|
|
490
|
+
const r = (al << (32 - num)) | (ah >>> num);
|
|
491
|
+
return r >>> 0;
|
|
492
|
+
}
|
|
493
|
+
function rotr64_lo(ah, al, num) {
|
|
494
|
+
const r = (ah << (32 - num)) | (al >>> num);
|
|
495
|
+
return r >>> 0;
|
|
496
|
+
}
|
|
497
|
+
function shr64_hi(ah, al, num) {
|
|
498
|
+
return ah >>> num;
|
|
499
|
+
}
|
|
500
|
+
function shr64_lo(ah, al, num) {
|
|
501
|
+
const r = (ah << (32 - num)) | (al >>> num);
|
|
502
|
+
return r >>> 0;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* An implementation of RIPEMD160 cryptographic hash function. Extends the BaseHash class.
|
|
506
|
+
* It provides a way to compute a 'digest' for any kind of input data; transforming the data
|
|
507
|
+
* into a unique output of fixed size. The output is deterministic; it will always be
|
|
508
|
+
* the same for the same input.
|
|
509
|
+
*
|
|
510
|
+
* @class RIPEMD160
|
|
511
|
+
* @param None
|
|
512
|
+
*
|
|
513
|
+
* @constructor
|
|
514
|
+
* Use the RIPEMD160 constructor to create an instance of RIPEMD160 hash function.
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* const ripemd160 = new RIPEMD160();
|
|
518
|
+
*
|
|
519
|
+
* @property h - Array that is updated iteratively as part of hashing computation.
|
|
520
|
+
*/
|
|
521
|
+
class RIPEMD160 extends BaseHash {
|
|
522
|
+
constructor() {
|
|
523
|
+
super(512, 160, 192, 64);
|
|
524
|
+
this.endian = 'little';
|
|
525
|
+
this.h = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
|
|
526
|
+
this.endian = 'little';
|
|
527
|
+
}
|
|
528
|
+
_update(msg, start) {
|
|
529
|
+
let A = this.h[0];
|
|
530
|
+
let B = this.h[1];
|
|
531
|
+
let C = this.h[2];
|
|
532
|
+
let D = this.h[3];
|
|
533
|
+
let E = this.h[4];
|
|
534
|
+
let Ah = A;
|
|
535
|
+
let Bh = B;
|
|
536
|
+
let Ch = C;
|
|
537
|
+
let Dh = D;
|
|
538
|
+
let Eh = E;
|
|
539
|
+
let T;
|
|
540
|
+
for (let j = 0; j < 80; j++) {
|
|
541
|
+
T = sum32(rotl32(SUM32_4(A, f(j, B, C, D), msg[r[j] + start], K(j)), s[j]), E);
|
|
542
|
+
A = E;
|
|
543
|
+
E = D;
|
|
544
|
+
D = rotl32(C, 10);
|
|
545
|
+
C = B;
|
|
546
|
+
B = T;
|
|
547
|
+
T = sum32(rotl32(SUM32_4(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j] + start], Kh(j)), sh[j]), Eh);
|
|
548
|
+
Ah = Eh;
|
|
549
|
+
Eh = Dh;
|
|
550
|
+
Dh = rotl32(Ch, 10);
|
|
551
|
+
Ch = Bh;
|
|
552
|
+
Bh = T;
|
|
553
|
+
}
|
|
554
|
+
T = SUM32_3(this.h[1], C, Dh);
|
|
555
|
+
this.h[1] = SUM32_3(this.h[2], D, Eh);
|
|
556
|
+
this.h[2] = SUM32_3(this.h[3], E, Ah);
|
|
557
|
+
this.h[3] = SUM32_3(this.h[4], A, Bh);
|
|
558
|
+
this.h[4] = SUM32_3(this.h[0], B, Ch);
|
|
559
|
+
this.h[0] = T;
|
|
560
|
+
}
|
|
561
|
+
_digest(enc) {
|
|
562
|
+
if (enc === 'hex') {
|
|
563
|
+
return toHex32(this.h, 'little');
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
return split32(this.h, 'little');
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
exports.RIPEMD160 = RIPEMD160;
|
|
571
|
+
/**
|
|
572
|
+
* An implementation of SHA256 cryptographic hash function. Extends the BaseHash class.
|
|
573
|
+
* It provides a way to compute a 'digest' for any kind of input data; transforming the data
|
|
574
|
+
* into a unique output of fixed size. The output is deterministic; it will always be
|
|
575
|
+
* the same for the same input.
|
|
576
|
+
*
|
|
577
|
+
* @class SHA256
|
|
578
|
+
* @param None
|
|
579
|
+
*
|
|
580
|
+
* @constructor
|
|
581
|
+
* Use the SHA256 constructor to create an instance of SHA256 hash function.
|
|
582
|
+
*
|
|
583
|
+
* @example
|
|
584
|
+
* const sha256 = new SHA256();
|
|
585
|
+
*
|
|
586
|
+
* @property h - The initial hash constants
|
|
587
|
+
* @property W - Provides a way to recycle usage of the array memory.
|
|
588
|
+
* @property k - The round constants used for each round of SHA-256
|
|
589
|
+
*/
|
|
590
|
+
class SHA256 extends BaseHash {
|
|
591
|
+
constructor() {
|
|
592
|
+
super(512, 256, 192, 64);
|
|
593
|
+
this.h = [
|
|
594
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
595
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
596
|
+
];
|
|
597
|
+
this.k = [
|
|
598
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
|
599
|
+
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
600
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
601
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
602
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
|
603
|
+
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
604
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
|
605
|
+
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
606
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
607
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
608
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
|
609
|
+
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
610
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
|
611
|
+
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
612
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
613
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
614
|
+
];
|
|
615
|
+
this.W = new Array(64);
|
|
616
|
+
}
|
|
617
|
+
_update(msg, start) {
|
|
618
|
+
const W = this.W;
|
|
619
|
+
let i;
|
|
620
|
+
for (i = 0; i < 16; i++) {
|
|
621
|
+
W[i] = msg[start + i];
|
|
622
|
+
}
|
|
623
|
+
for (; i < W.length; i++) {
|
|
624
|
+
W[i] = SUM32_4(G1_256(W[i - 2]), W[i - 7], G0_256(W[i - 15]), W[i - 16]);
|
|
625
|
+
}
|
|
626
|
+
let a = this.h[0];
|
|
627
|
+
let b = this.h[1];
|
|
628
|
+
let c = this.h[2];
|
|
629
|
+
let d = this.h[3];
|
|
630
|
+
let e = this.h[4];
|
|
631
|
+
let f = this.h[5];
|
|
632
|
+
let g = this.h[6];
|
|
633
|
+
let h = this.h[7];
|
|
634
|
+
assert(this.k.length === W.length);
|
|
635
|
+
for (i = 0; i < W.length; i++) {
|
|
636
|
+
const T1 = SUM32_5(h, S1_256(e), ch32(e, f, g), this.k[i], W[i]);
|
|
637
|
+
const T2 = sum32(S0_256(a), maj32(a, b, c));
|
|
638
|
+
h = g;
|
|
639
|
+
g = f;
|
|
640
|
+
f = e;
|
|
641
|
+
e = sum32(d, T1);
|
|
642
|
+
d = c;
|
|
643
|
+
c = b;
|
|
644
|
+
b = a;
|
|
645
|
+
a = sum32(T1, T2);
|
|
646
|
+
}
|
|
647
|
+
this.h[0] = sum32(this.h[0], a);
|
|
648
|
+
this.h[1] = sum32(this.h[1], b);
|
|
649
|
+
this.h[2] = sum32(this.h[2], c);
|
|
650
|
+
this.h[3] = sum32(this.h[3], d);
|
|
651
|
+
this.h[4] = sum32(this.h[4], e);
|
|
652
|
+
this.h[5] = sum32(this.h[5], f);
|
|
653
|
+
this.h[6] = sum32(this.h[6], g);
|
|
654
|
+
this.h[7] = sum32(this.h[7], h);
|
|
655
|
+
}
|
|
656
|
+
;
|
|
657
|
+
_digest(enc) {
|
|
658
|
+
if (enc === 'hex') {
|
|
659
|
+
return toHex32(this.h, 'big');
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
return split32(this.h, 'big');
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
exports.SHA256 = SHA256;
|
|
667
|
+
/**
|
|
668
|
+
* An implementation of SHA1 cryptographic hash function. Extends the BaseHash class.
|
|
669
|
+
* It provides a way to compute a 'digest' for any kind of input data; transforming the data
|
|
670
|
+
* into a unique output of fixed size. The output is deterministic; it will always be
|
|
671
|
+
* the same for the same input.
|
|
672
|
+
*
|
|
673
|
+
* @class SHA1
|
|
674
|
+
* @param None
|
|
675
|
+
*
|
|
676
|
+
* @constructor
|
|
677
|
+
* Use the SHA1 constructor to create an instance of SHA1 hash function.
|
|
678
|
+
*
|
|
679
|
+
* @example
|
|
680
|
+
* const sha1 = new SHA1();
|
|
681
|
+
*
|
|
682
|
+
* @property h - The initial hash constants.
|
|
683
|
+
* @property W - Provides a way to recycle usage of the array memory.
|
|
684
|
+
* @property k - The round constants used for each round of SHA-1.
|
|
685
|
+
*/
|
|
686
|
+
class SHA1 extends BaseHash {
|
|
687
|
+
constructor() {
|
|
688
|
+
super(512, 160, 80, 64);
|
|
689
|
+
this.k = [
|
|
690
|
+
0x5A827999, 0x6ED9EBA1,
|
|
691
|
+
0x8F1BBCDC, 0xCA62C1D6
|
|
692
|
+
];
|
|
693
|
+
this.h = [
|
|
694
|
+
0x67452301, 0xefcdab89, 0x98badcfe,
|
|
695
|
+
0x10325476, 0xc3d2e1f0
|
|
696
|
+
];
|
|
697
|
+
this.W = new Array(80);
|
|
698
|
+
}
|
|
699
|
+
_update(msg, start) {
|
|
700
|
+
const W = this.W;
|
|
701
|
+
let i;
|
|
702
|
+
for (i = 0; i < 16; i++) {
|
|
703
|
+
W[i] = msg[start + i];
|
|
704
|
+
}
|
|
705
|
+
for (; i < W.length; i++) {
|
|
706
|
+
W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
|
|
707
|
+
}
|
|
708
|
+
let a = this.h[0];
|
|
709
|
+
let b = this.h[1];
|
|
710
|
+
let c = this.h[2];
|
|
711
|
+
let d = this.h[3];
|
|
712
|
+
let e = this.h[4];
|
|
713
|
+
for (i = 0; i < W.length; i++) {
|
|
714
|
+
const s = ~~(i / 20);
|
|
715
|
+
const t = SUM32_5(rotl32(a, 5), FT_1(s, b, c, d), e, W[i], this.k[s]);
|
|
716
|
+
e = d;
|
|
717
|
+
d = c;
|
|
718
|
+
c = rotl32(b, 30);
|
|
719
|
+
b = a;
|
|
720
|
+
a = t;
|
|
721
|
+
}
|
|
722
|
+
this.h[0] = sum32(this.h[0], a);
|
|
723
|
+
this.h[1] = sum32(this.h[1], b);
|
|
724
|
+
this.h[2] = sum32(this.h[2], c);
|
|
725
|
+
this.h[3] = sum32(this.h[3], d);
|
|
726
|
+
this.h[4] = sum32(this.h[4], e);
|
|
727
|
+
}
|
|
728
|
+
_digest(enc) {
|
|
729
|
+
if (enc === 'hex') {
|
|
730
|
+
return toHex32(this.h, 'big');
|
|
731
|
+
}
|
|
732
|
+
else {
|
|
733
|
+
return split32(this.h, 'big');
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
exports.SHA1 = SHA1;
|
|
738
|
+
/**
|
|
739
|
+
* An implementation of SHA512 cryptographic hash function. Extends the BaseHash class.
|
|
740
|
+
* It provides a way to compute a 'digest' for any kind of input data; transforming the data
|
|
741
|
+
* into a unique output of fixed size. The output is deterministic; it will always be
|
|
742
|
+
* the same for the same input.
|
|
743
|
+
*
|
|
744
|
+
* @class SHA512
|
|
745
|
+
* @param None
|
|
746
|
+
*
|
|
747
|
+
* @constructor
|
|
748
|
+
* Use the SHA512 constructor to create an instance of SHA512 hash function.
|
|
749
|
+
*
|
|
750
|
+
* @example
|
|
751
|
+
* const sha512 = new SHA512();
|
|
752
|
+
*
|
|
753
|
+
* @property h - The initial hash constants.
|
|
754
|
+
* @property W - Provides a way to recycle usage of the array memory.
|
|
755
|
+
* @property k - The round constants used for each round of SHA-512.
|
|
756
|
+
*/
|
|
757
|
+
class SHA512 extends BaseHash {
|
|
758
|
+
constructor() {
|
|
759
|
+
super(1024, 512, 192, 128);
|
|
760
|
+
this.h = [
|
|
761
|
+
0x6a09e667, 0xf3bcc908,
|
|
762
|
+
0xbb67ae85, 0x84caa73b,
|
|
763
|
+
0x3c6ef372, 0xfe94f82b,
|
|
764
|
+
0xa54ff53a, 0x5f1d36f1,
|
|
765
|
+
0x510e527f, 0xade682d1,
|
|
766
|
+
0x9b05688c, 0x2b3e6c1f,
|
|
767
|
+
0x1f83d9ab, 0xfb41bd6b,
|
|
768
|
+
0x5be0cd19, 0x137e2179
|
|
769
|
+
];
|
|
770
|
+
this.k = [
|
|
771
|
+
0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
|
|
772
|
+
0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
|
|
773
|
+
0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
|
|
774
|
+
0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
|
|
775
|
+
0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
|
|
776
|
+
0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
|
|
777
|
+
0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
|
|
778
|
+
0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
|
|
779
|
+
0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
|
|
780
|
+
0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
|
|
781
|
+
0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
|
|
782
|
+
0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
|
|
783
|
+
0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
|
|
784
|
+
0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
|
|
785
|
+
0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
|
|
786
|
+
0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
|
|
787
|
+
0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
|
|
788
|
+
0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
|
|
789
|
+
0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
|
|
790
|
+
0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
|
|
791
|
+
0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
|
|
792
|
+
0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
|
|
793
|
+
0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
|
|
794
|
+
0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
|
|
795
|
+
0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
|
|
796
|
+
0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
|
|
797
|
+
0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
|
|
798
|
+
0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
|
|
799
|
+
0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
|
|
800
|
+
0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
|
|
801
|
+
0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
|
|
802
|
+
0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
|
|
803
|
+
0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
|
|
804
|
+
0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
|
|
805
|
+
0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
|
|
806
|
+
0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
|
|
807
|
+
0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
|
|
808
|
+
0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
|
|
809
|
+
0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
|
|
810
|
+
0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
|
|
811
|
+
];
|
|
812
|
+
this.W = new Array(160);
|
|
813
|
+
}
|
|
814
|
+
_prepareBlock(msg, start) {
|
|
815
|
+
const W = this.W;
|
|
816
|
+
// 32 x 32bit words
|
|
817
|
+
let i;
|
|
818
|
+
for (i = 0; i < 32; i++)
|
|
819
|
+
W[i] = msg[start + i];
|
|
820
|
+
for (; i < W.length; i += 2) {
|
|
821
|
+
const c0_hi = g1_512_hi(W[i - 4], W[i - 3]); // i - 2
|
|
822
|
+
const c0_lo = g1_512_lo(W[i - 4], W[i - 3]);
|
|
823
|
+
const c1_hi = W[i - 14]; // i - 7
|
|
824
|
+
const c1_lo = W[i - 13];
|
|
825
|
+
const c2_hi = g0_512_hi(W[i - 30], W[i - 29]); // i - 15
|
|
826
|
+
const c2_lo = g0_512_lo(W[i - 30], W[i - 29]);
|
|
827
|
+
const c3_hi = W[i - 32]; // i - 16
|
|
828
|
+
const c3_lo = W[i - 31];
|
|
829
|
+
W[i] = sum64_4_hi(c0_hi, c0_lo, c1_hi, c1_lo, c2_hi, c2_lo, c3_hi, c3_lo);
|
|
830
|
+
W[i + 1] = sum64_4_lo(c0_hi, c0_lo, c1_hi, c1_lo, c2_hi, c2_lo, c3_hi, c3_lo);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
_update(msg, start) {
|
|
834
|
+
this._prepareBlock(msg, start);
|
|
835
|
+
const W = this.W;
|
|
836
|
+
let ah = this.h[0];
|
|
837
|
+
let al = this.h[1];
|
|
838
|
+
let bh = this.h[2];
|
|
839
|
+
let bl = this.h[3];
|
|
840
|
+
let ch = this.h[4];
|
|
841
|
+
let cl = this.h[5];
|
|
842
|
+
let dh = this.h[6];
|
|
843
|
+
let dl = this.h[7];
|
|
844
|
+
let eh = this.h[8];
|
|
845
|
+
let el = this.h[9];
|
|
846
|
+
let fh = this.h[10];
|
|
847
|
+
let fl = this.h[11];
|
|
848
|
+
let gh = this.h[12];
|
|
849
|
+
let gl = this.h[13];
|
|
850
|
+
let hh = this.h[14];
|
|
851
|
+
let hl = this.h[15];
|
|
852
|
+
assert(this.k.length === W.length);
|
|
853
|
+
for (let i = 0; i < W.length; i += 2) {
|
|
854
|
+
let c0_hi = hh;
|
|
855
|
+
let c0_lo = hl;
|
|
856
|
+
let c1_hi = s1_512_hi(eh, el);
|
|
857
|
+
let c1_lo = s1_512_lo(eh, el);
|
|
858
|
+
const c2_hi = ch64_hi(eh, el, fh, fl, gh, gl);
|
|
859
|
+
const c2_lo = ch64_lo(eh, el, fh, fl, gh, gl);
|
|
860
|
+
const c3_hi = this.k[i];
|
|
861
|
+
const c3_lo = this.k[i + 1];
|
|
862
|
+
const c4_hi = W[i];
|
|
863
|
+
const c4_lo = W[i + 1];
|
|
864
|
+
const T1_hi = sum64_5_hi(c0_hi, c0_lo, c1_hi, c1_lo, c2_hi, c2_lo, c3_hi, c3_lo, c4_hi, c4_lo);
|
|
865
|
+
const T1_lo = sum64_5_lo(c0_hi, c0_lo, c1_hi, c1_lo, c2_hi, c2_lo, c3_hi, c3_lo, c4_hi, c4_lo);
|
|
866
|
+
c0_hi = s0_512_hi(ah, al);
|
|
867
|
+
c0_lo = s0_512_lo(ah, al);
|
|
868
|
+
c1_hi = maj64_hi(ah, al, bh, bl, ch, cl);
|
|
869
|
+
c1_lo = maj64_lo(ah, al, bh, bl, ch, cl);
|
|
870
|
+
const T2_hi = sum64_hi(c0_hi, c0_lo, c1_hi, c1_lo);
|
|
871
|
+
const T2_lo = sum64_lo(c0_hi, c0_lo, c1_hi, c1_lo);
|
|
872
|
+
hh = gh;
|
|
873
|
+
hl = gl;
|
|
874
|
+
gh = fh;
|
|
875
|
+
gl = fl;
|
|
876
|
+
fh = eh;
|
|
877
|
+
fl = el;
|
|
878
|
+
eh = sum64_hi(dh, dl, T1_hi, T1_lo);
|
|
879
|
+
el = sum64_lo(dl, dl, T1_hi, T1_lo);
|
|
880
|
+
dh = ch;
|
|
881
|
+
dl = cl;
|
|
882
|
+
ch = bh;
|
|
883
|
+
cl = bl;
|
|
884
|
+
bh = ah;
|
|
885
|
+
bl = al;
|
|
886
|
+
ah = sum64_hi(T1_hi, T1_lo, T2_hi, T2_lo);
|
|
887
|
+
al = sum64_lo(T1_hi, T1_lo, T2_hi, T2_lo);
|
|
888
|
+
}
|
|
889
|
+
sum64(this.h, 0, ah, al);
|
|
890
|
+
sum64(this.h, 2, bh, bl);
|
|
891
|
+
sum64(this.h, 4, ch, cl);
|
|
892
|
+
sum64(this.h, 6, dh, dl);
|
|
893
|
+
sum64(this.h, 8, eh, el);
|
|
894
|
+
sum64(this.h, 10, fh, fl);
|
|
895
|
+
sum64(this.h, 12, gh, gl);
|
|
896
|
+
sum64(this.h, 14, hh, hl);
|
|
897
|
+
}
|
|
898
|
+
_digest(enc) {
|
|
899
|
+
if (enc === 'hex')
|
|
900
|
+
return toHex32(this.h, 'big');
|
|
901
|
+
else
|
|
902
|
+
return split32(this.h, 'big');
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
exports.SHA512 = SHA512;
|
|
906
|
+
function ch64_hi(xh, xl, yh, yl, zh, zl) {
|
|
907
|
+
let r = (xh & yh) ^ ((~xh) & zh);
|
|
908
|
+
if (r < 0)
|
|
909
|
+
r += 0x100000000;
|
|
910
|
+
return r;
|
|
911
|
+
}
|
|
912
|
+
function ch64_lo(xh, xl, yh, yl, zh, zl) {
|
|
913
|
+
let r = (xl & yl) ^ ((~xl) & zl);
|
|
914
|
+
if (r < 0)
|
|
915
|
+
r += 0x100000000;
|
|
916
|
+
return r;
|
|
917
|
+
}
|
|
918
|
+
function maj64_hi(xh, xl, yh, yl, zh, zl) {
|
|
919
|
+
let r = (xh & yh) ^ (xh & zh) ^ (yh & zh);
|
|
920
|
+
if (r < 0)
|
|
921
|
+
r += 0x100000000;
|
|
922
|
+
return r;
|
|
923
|
+
}
|
|
924
|
+
function maj64_lo(xh, xl, yh, yl, zh, zl) {
|
|
925
|
+
let r = (xl & yl) ^ (xl & zl) ^ (yl & zl);
|
|
926
|
+
if (r < 0)
|
|
927
|
+
r += 0x100000000;
|
|
928
|
+
return r;
|
|
929
|
+
}
|
|
930
|
+
function s0_512_hi(xh, xl) {
|
|
931
|
+
const c0_hi = rotr64_hi(xh, xl, 28);
|
|
932
|
+
const c1_hi = rotr64_hi(xl, xh, 2); // 34
|
|
933
|
+
const c2_hi = rotr64_hi(xl, xh, 7); // 39
|
|
934
|
+
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
935
|
+
if (r < 0)
|
|
936
|
+
r += 0x100000000;
|
|
937
|
+
return r;
|
|
938
|
+
}
|
|
939
|
+
function s0_512_lo(xh, xl) {
|
|
940
|
+
const c0_lo = rotr64_lo(xh, xl, 28);
|
|
941
|
+
const c1_lo = rotr64_lo(xl, xh, 2); // 34
|
|
942
|
+
const c2_lo = rotr64_lo(xl, xh, 7); // 39
|
|
943
|
+
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
944
|
+
if (r < 0)
|
|
945
|
+
r += 0x100000000;
|
|
946
|
+
return r;
|
|
947
|
+
}
|
|
948
|
+
function s1_512_hi(xh, xl) {
|
|
949
|
+
const c0_hi = rotr64_hi(xh, xl, 14);
|
|
950
|
+
const c1_hi = rotr64_hi(xh, xl, 18);
|
|
951
|
+
const c2_hi = rotr64_hi(xl, xh, 9); // 41
|
|
952
|
+
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
953
|
+
if (r < 0)
|
|
954
|
+
r += 0x100000000;
|
|
955
|
+
return r;
|
|
956
|
+
}
|
|
957
|
+
function s1_512_lo(xh, xl) {
|
|
958
|
+
const c0_lo = rotr64_lo(xh, xl, 14);
|
|
959
|
+
const c1_lo = rotr64_lo(xh, xl, 18);
|
|
960
|
+
const c2_lo = rotr64_lo(xl, xh, 9); // 41
|
|
961
|
+
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
962
|
+
if (r < 0)
|
|
963
|
+
r += 0x100000000;
|
|
964
|
+
return r;
|
|
965
|
+
}
|
|
966
|
+
function g0_512_hi(xh, xl) {
|
|
967
|
+
const c0_hi = rotr64_hi(xh, xl, 1);
|
|
968
|
+
const c1_hi = rotr64_hi(xh, xl, 8);
|
|
969
|
+
const c2_hi = shr64_hi(xh, xl, 7);
|
|
970
|
+
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
971
|
+
if (r < 0)
|
|
972
|
+
r += 0x100000000;
|
|
973
|
+
return r;
|
|
974
|
+
}
|
|
975
|
+
function g0_512_lo(xh, xl) {
|
|
976
|
+
const c0_lo = rotr64_lo(xh, xl, 1);
|
|
977
|
+
const c1_lo = rotr64_lo(xh, xl, 8);
|
|
978
|
+
const c2_lo = shr64_lo(xh, xl, 7);
|
|
979
|
+
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
980
|
+
if (r < 0)
|
|
981
|
+
r += 0x100000000;
|
|
982
|
+
return r;
|
|
983
|
+
}
|
|
984
|
+
function g1_512_hi(xh, xl) {
|
|
985
|
+
const c0_hi = rotr64_hi(xh, xl, 19);
|
|
986
|
+
const c1_hi = rotr64_hi(xl, xh, 29); // 61
|
|
987
|
+
const c2_hi = shr64_hi(xh, xl, 6);
|
|
988
|
+
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
989
|
+
if (r < 0)
|
|
990
|
+
r += 0x100000000;
|
|
991
|
+
return r;
|
|
992
|
+
}
|
|
993
|
+
function g1_512_lo(xh, xl) {
|
|
994
|
+
const c0_lo = rotr64_lo(xh, xl, 19);
|
|
995
|
+
const c1_lo = rotr64_lo(xl, xh, 29); // 61
|
|
996
|
+
const c2_lo = shr64_lo(xh, xl, 6);
|
|
997
|
+
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
998
|
+
if (r < 0)
|
|
999
|
+
r += 0x100000000;
|
|
1000
|
+
return r;
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* The `SHA256HMAC` class is used to create Hash-based Message Authentication Code (HMAC) using the SHA-256 cryptographic hash function.
|
|
1004
|
+
*
|
|
1005
|
+
* HMAC is a specific type of MAC involving a cryptographic hash function and a secret cryptographic key. It may be used to simultaneously verify both the data integrity and the authenticity of a message.
|
|
1006
|
+
*
|
|
1007
|
+
* This class also uses the SHA-256 cryptographic hash algorithm that produces a 256-bit (32-byte) hash value.
|
|
1008
|
+
*
|
|
1009
|
+
* @property inner - Represents the inner hash of SHA-256.
|
|
1010
|
+
* @property outer - Represents the outer hash of SHA-256.
|
|
1011
|
+
* @property blockSize - The block size for the SHA-256 hash function, in bytes. It's set to 64 bytes.
|
|
1012
|
+
* @property outSize - The output size of the SHA-256 hash function, in bytes. It's set to 32 bytes.
|
|
1013
|
+
*/
|
|
1014
|
+
class SHA256HMAC {
|
|
1015
|
+
/**
|
|
1016
|
+
* The constructor for the `SHA256HMAC` class.
|
|
1017
|
+
*
|
|
1018
|
+
* It initializes the `SHA256HMAC` object and sets up the inner and outer padded keys.
|
|
1019
|
+
* If the key size is larger than the blockSize, it is digested using SHA-256.
|
|
1020
|
+
* If the key size is less than the blockSize, it is padded with zeroes.
|
|
1021
|
+
*
|
|
1022
|
+
* @constructor
|
|
1023
|
+
* @param key - The key to use to create the HMAC. Can be a number array or a string in hexadecimal format.
|
|
1024
|
+
*
|
|
1025
|
+
* @example
|
|
1026
|
+
* const myHMAC = new SHA256HMAC('deadbeef');
|
|
1027
|
+
*/
|
|
1028
|
+
constructor(key) {
|
|
1029
|
+
this.blockSize = 64;
|
|
1030
|
+
this.outSize = 32;
|
|
1031
|
+
key = toArray(key, 'hex');
|
|
1032
|
+
// Shorten key, if needed
|
|
1033
|
+
if (key.length > this.blockSize) {
|
|
1034
|
+
key = new SHA256().update(key).digest();
|
|
1035
|
+
}
|
|
1036
|
+
assert(key.length <= this.blockSize);
|
|
1037
|
+
// Add padding to key
|
|
1038
|
+
let i;
|
|
1039
|
+
for (i = key.length; i < this.blockSize; i++) {
|
|
1040
|
+
key.push(0);
|
|
1041
|
+
}
|
|
1042
|
+
for (i = 0; i < key.length; i++) {
|
|
1043
|
+
key[i] ^= 0x36;
|
|
1044
|
+
}
|
|
1045
|
+
this.inner = new SHA256().update(key);
|
|
1046
|
+
// 0x36 ^ 0x5c = 0x6a
|
|
1047
|
+
for (i = 0; i < key.length; i++) {
|
|
1048
|
+
key[i] ^= 0x6a;
|
|
1049
|
+
}
|
|
1050
|
+
this.outer = new SHA256().update(key);
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Updates the `SHA256HMAC` object with part of the message to be hashed.
|
|
1054
|
+
*
|
|
1055
|
+
* @method update
|
|
1056
|
+
* @param msg - Part of the message to hash. Can be a number array or a string.
|
|
1057
|
+
* @param enc - If 'hex', then the input is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
|
|
1058
|
+
* @returns Returns the instance of `SHA256HMAC` for chaining calls.
|
|
1059
|
+
*
|
|
1060
|
+
* @example
|
|
1061
|
+
* myHMAC.update('deadbeef', 'hex');
|
|
1062
|
+
*/
|
|
1063
|
+
update(msg, enc) {
|
|
1064
|
+
this.inner.update(msg, enc);
|
|
1065
|
+
return this;
|
|
1066
|
+
}
|
|
1067
|
+
/**
|
|
1068
|
+
* Finalizes the HMAC computation and returns the resultant hash.
|
|
1069
|
+
*
|
|
1070
|
+
* @method digest
|
|
1071
|
+
* @param enc - If 'hex', then the output is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
|
|
1072
|
+
* @returns Returns the digest of the hashed data. Can be a number array or a string.
|
|
1073
|
+
*
|
|
1074
|
+
* @example
|
|
1075
|
+
* let hashedMessage = myHMAC.digest('hex');
|
|
1076
|
+
*/
|
|
1077
|
+
digest(enc) {
|
|
1078
|
+
this.outer.update(this.inner.digest());
|
|
1079
|
+
return this.outer.digest(enc);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
exports.SHA256HMAC = SHA256HMAC;
|
|
1083
|
+
/**
|
|
1084
|
+
* The `SHA512HMAC` class is used to create Hash-based Message Authentication Code (HMAC) using the SHA-512 cryptographic hash function.
|
|
1085
|
+
*
|
|
1086
|
+
* HMAC is a specific type of MAC involving a cryptographic hash function and a secret cryptographic key. It may be used to simultaneously verify both the data integrity and the authenticity of a message.
|
|
1087
|
+
*
|
|
1088
|
+
* This class also uses the SHA-512 cryptographic hash algorithm that produces a 512-bit (64-byte) hash value.
|
|
1089
|
+
*
|
|
1090
|
+
* @property inner - Represents the inner hash of SHA-512.
|
|
1091
|
+
* @property outer - Represents the outer hash of SHA-512.
|
|
1092
|
+
* @property blockSize - The block size for the SHA-512 hash function, in bytes. It's set to 128 bytes.
|
|
1093
|
+
* @property outSize - The output size of the SHA-512 hash function, in bytes. It's set to 64 bytes.
|
|
1094
|
+
*/
|
|
1095
|
+
class SHA512HMAC {
|
|
1096
|
+
/**
|
|
1097
|
+
* The constructor for the `SHA512HMAC` class.
|
|
1098
|
+
*
|
|
1099
|
+
* It initializes the `SHA512HMAC` object and sets up the inner and outer padded keys.
|
|
1100
|
+
* If the key size is larger than the blockSize, it is digested using SHA-512.
|
|
1101
|
+
* If the key size is less than the blockSize, it is padded with zeroes.
|
|
1102
|
+
*
|
|
1103
|
+
* @constructor
|
|
1104
|
+
* @param key - The key to use to create the HMAC. Can be a number array or a string in hexadecimal format.
|
|
1105
|
+
*
|
|
1106
|
+
* @example
|
|
1107
|
+
* const myHMAC = new SHA512HMAC('deadbeef');
|
|
1108
|
+
*/
|
|
1109
|
+
constructor(key) {
|
|
1110
|
+
this.blockSize = 128;
|
|
1111
|
+
this.outSize = 32;
|
|
1112
|
+
key = toArray(key, 'hex');
|
|
1113
|
+
// Shorten key, if needed
|
|
1114
|
+
if (key.length > this.blockSize) {
|
|
1115
|
+
key = new SHA512().update(key).digest();
|
|
1116
|
+
}
|
|
1117
|
+
assert(key.length <= this.blockSize);
|
|
1118
|
+
// Add padding to key
|
|
1119
|
+
let i;
|
|
1120
|
+
for (i = key.length; i < this.blockSize; i++) {
|
|
1121
|
+
key.push(0);
|
|
1122
|
+
}
|
|
1123
|
+
for (i = 0; i < key.length; i++) {
|
|
1124
|
+
key[i] ^= 0x36;
|
|
1125
|
+
}
|
|
1126
|
+
this.inner = new SHA512().update(key);
|
|
1127
|
+
// 0x36 ^ 0x5c = 0x6a
|
|
1128
|
+
for (i = 0; i < key.length; i++) {
|
|
1129
|
+
key[i] ^= 0x6a;
|
|
1130
|
+
}
|
|
1131
|
+
this.outer = new SHA512().update(key);
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Updates the `SHA512HMAC` object with part of the message to be hashed.
|
|
1135
|
+
*
|
|
1136
|
+
* @method update
|
|
1137
|
+
* @param msg - Part of the message to hash. Can be a number array or a string.
|
|
1138
|
+
* @param enc - If 'hex', then the input is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
|
|
1139
|
+
* @returns Returns the instance of `SHA512HMAC` for chaining calls.
|
|
1140
|
+
*
|
|
1141
|
+
* @example
|
|
1142
|
+
* myHMAC.update('deadbeef', 'hex');
|
|
1143
|
+
*/
|
|
1144
|
+
update(msg, enc) {
|
|
1145
|
+
this.inner.update(msg, enc);
|
|
1146
|
+
return this;
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Finalizes the HMAC computation and returns the resultant hash.
|
|
1150
|
+
*
|
|
1151
|
+
* @method digest
|
|
1152
|
+
* @param enc - If 'hex', then the output is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
|
|
1153
|
+
* @returns Returns the digest of the hashed data. Can be a number array or a string.
|
|
1154
|
+
*
|
|
1155
|
+
* @example
|
|
1156
|
+
* let hashedMessage = myHMAC.digest('hex');
|
|
1157
|
+
*/
|
|
1158
|
+
digest(enc) {
|
|
1159
|
+
this.outer.update(this.inner.digest());
|
|
1160
|
+
return this.outer.digest(enc);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
exports.SHA512HMAC = SHA512HMAC;
|
|
1164
|
+
/**
|
|
1165
|
+
* Computes RIPEMD160 hash of a given message.
|
|
1166
|
+
* @function ripemd160
|
|
1167
|
+
* @param msg - The message to compute the hash for.
|
|
1168
|
+
* @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
|
|
1169
|
+
*
|
|
1170
|
+
* @returns the computed RIPEMD160 hash of the message.
|
|
1171
|
+
*
|
|
1172
|
+
* @example
|
|
1173
|
+
* const digest = ripemd160('Hello, world!');
|
|
1174
|
+
*/
|
|
1175
|
+
const ripemd160 = (msg, enc) => {
|
|
1176
|
+
return new RIPEMD160().update(msg, enc).digest(enc);
|
|
1177
|
+
};
|
|
1178
|
+
exports.ripemd160 = ripemd160;
|
|
1179
|
+
/**
|
|
1180
|
+
* Computes SHA1 hash of a given message.
|
|
1181
|
+
* @function sha1
|
|
1182
|
+
* @param msg - The message to compute the hash for.
|
|
1183
|
+
* @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
|
|
1184
|
+
*
|
|
1185
|
+
* @returns the computed SHA1 hash of the message.
|
|
1186
|
+
*
|
|
1187
|
+
* @example
|
|
1188
|
+
* const digest = sha1('Hello, world!');
|
|
1189
|
+
*/
|
|
1190
|
+
const sha1 = (msg, enc) => {
|
|
1191
|
+
return new SHA1().update(msg, enc).digest(enc);
|
|
1192
|
+
};
|
|
1193
|
+
exports.sha1 = sha1;
|
|
1194
|
+
/**
|
|
1195
|
+
* Computes SHA256 hash of a given message.
|
|
1196
|
+
* @function sha256
|
|
1197
|
+
* @param msg - The message to compute the hash for.
|
|
1198
|
+
* @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
|
|
1199
|
+
*
|
|
1200
|
+
* @returns the computed SHA256 hash of the message.
|
|
1201
|
+
*
|
|
1202
|
+
* @example
|
|
1203
|
+
* const digest = sha256('Hello, world!');
|
|
1204
|
+
*/
|
|
1205
|
+
const sha256 = (msg, enc) => {
|
|
1206
|
+
return new SHA256().update(msg, enc).digest(enc);
|
|
1207
|
+
};
|
|
1208
|
+
exports.sha256 = sha256;
|
|
1209
|
+
/**
|
|
1210
|
+
* Computes SHA512 hash of a given message.
|
|
1211
|
+
* @function sha512
|
|
1212
|
+
* @param msg - The message to compute the hash for.
|
|
1213
|
+
* @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
|
|
1214
|
+
*
|
|
1215
|
+
* @returns the computed SHA512 hash of the message.
|
|
1216
|
+
*
|
|
1217
|
+
* @example
|
|
1218
|
+
* const digest = sha512('Hello, world!');
|
|
1219
|
+
*/
|
|
1220
|
+
const sha512 = (msg, enc) => {
|
|
1221
|
+
return new SHA512().update(msg, enc).digest(enc);
|
|
1222
|
+
};
|
|
1223
|
+
exports.sha512 = sha512;
|
|
1224
|
+
/**
|
|
1225
|
+
* Performs a 'double hash' using SHA256. This means the data is hashed twice
|
|
1226
|
+
* with SHA256. First, the SHA256 hash of the message is computed, then the
|
|
1227
|
+
* SHA256 hash of the resulting hash is computed.
|
|
1228
|
+
* @function hash256
|
|
1229
|
+
* @param msg - The message to compute the hash for.
|
|
1230
|
+
* @param enc - Encoding of the message.If 'hex', the message is decoded from hexadecimal.
|
|
1231
|
+
*
|
|
1232
|
+
* @returns the double hashed SHA256 output.
|
|
1233
|
+
*
|
|
1234
|
+
* @example
|
|
1235
|
+
* const doubleHash = hash256('Hello, world!');
|
|
1236
|
+
*/
|
|
1237
|
+
const hash256 = (msg, enc) => {
|
|
1238
|
+
const first = new SHA256().update(msg, enc).digest();
|
|
1239
|
+
return new SHA256().update(first).digest(enc);
|
|
1240
|
+
};
|
|
1241
|
+
exports.hash256 = hash256;
|
|
1242
|
+
/**
|
|
1243
|
+
* Computes SHA256 hash of a given message and then computes a RIPEMD160 hash of the result.
|
|
1244
|
+
*
|
|
1245
|
+
* @function hash160
|
|
1246
|
+
* @param msg - The message to compute the hash for.
|
|
1247
|
+
* @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal.
|
|
1248
|
+
*
|
|
1249
|
+
* @returns the RIPEMD160 hash of the SHA256 hash of the input message.
|
|
1250
|
+
*
|
|
1251
|
+
* @example
|
|
1252
|
+
* const hash = hash160('Hello, world!');
|
|
1253
|
+
*/
|
|
1254
|
+
const hash160 = (msg, enc) => {
|
|
1255
|
+
const first = new SHA256().update(msg, enc).digest();
|
|
1256
|
+
return new RIPEMD160().update(first).digest(enc);
|
|
1257
|
+
};
|
|
1258
|
+
exports.hash160 = hash160;
|
|
1259
|
+
/**
|
|
1260
|
+
* Computes SHA256 HMAC of a given message with a given key.
|
|
1261
|
+
* @function sha256hmac
|
|
1262
|
+
* @param key - The key used to compute the HMAC
|
|
1263
|
+
* @param msg - The message to compute the hash for.
|
|
1264
|
+
* @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
|
|
1265
|
+
*
|
|
1266
|
+
* @returns the computed HMAC of the message.
|
|
1267
|
+
*
|
|
1268
|
+
* @example
|
|
1269
|
+
* const digest = sha256hmac('deadbeef', 'ffff001d');
|
|
1270
|
+
*/
|
|
1271
|
+
const sha256hmac = (key, msg, enc) => {
|
|
1272
|
+
return new SHA256HMAC(key).update(msg, enc).digest(enc);
|
|
1273
|
+
};
|
|
1274
|
+
exports.sha256hmac = sha256hmac;
|
|
1275
|
+
/**
|
|
1276
|
+
* Computes SHA512 HMAC of a given message with a given key.
|
|
1277
|
+
* @function sha512hmac
|
|
1278
|
+
* @param key - The key used to compute the HMAC
|
|
1279
|
+
* @param msg - The message to compute the hash for.
|
|
1280
|
+
* @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
|
|
1281
|
+
*
|
|
1282
|
+
* @returns the computed HMAC of the message.
|
|
1283
|
+
*
|
|
1284
|
+
* @example
|
|
1285
|
+
* const digest = sha512hmac('deadbeef', 'ffff001d');
|
|
1286
|
+
*/
|
|
1287
|
+
const sha512hmac = (key, msg, enc) => {
|
|
1288
|
+
return new SHA512HMAC(key).update(msg, enc).digest(enc);
|
|
1289
|
+
};
|
|
1290
|
+
exports.sha512hmac = sha512hmac;
|
|
1291
|
+
/**
|
|
1292
|
+
* Limited SHA-512-only PBKDF2 function for use in deprecated BIP39 code.
|
|
1293
|
+
* @function pbkdf2
|
|
1294
|
+
* @param password - The PBKDF2 password
|
|
1295
|
+
* @param salt - The PBKDF2 salt
|
|
1296
|
+
* @param iterations - The number of of iterations to run
|
|
1297
|
+
* @param keylen - The length of the key
|
|
1298
|
+
* @param digest - The digest (must be sha512 for this implementation)
|
|
1299
|
+
*
|
|
1300
|
+
* @returns The computed key
|
|
1301
|
+
*/
|
|
1302
|
+
function pbkdf2(password, salt, iterations, keylen, digest = 'sha512') {
|
|
1303
|
+
if (digest !== 'sha512') {
|
|
1304
|
+
throw new Error('Only sha512 is supported in this PBKDF2 implementation');
|
|
1305
|
+
}
|
|
1306
|
+
const DK = new Array(keylen);
|
|
1307
|
+
const block1 = [...salt, 0, 0, 0, 0];
|
|
1308
|
+
const s = Buffer.from('hello');
|
|
1309
|
+
let destPos = 0;
|
|
1310
|
+
const hLen = 64;
|
|
1311
|
+
const l = Math.ceil(keylen / hLen);
|
|
1312
|
+
for (let i = 1; i <= l; i++) {
|
|
1313
|
+
block1[salt.length] = (i >> 24) & 0xFF; // MSB
|
|
1314
|
+
block1[salt.length + 1] = (i >> 16) & 0xFF;
|
|
1315
|
+
block1[salt.length + 2] = (i >> 8) & 0xFF;
|
|
1316
|
+
block1[salt.length + 3] = i & 0xFF; // LSB
|
|
1317
|
+
const T = (0, exports.sha512hmac)(password, block1);
|
|
1318
|
+
let U = T;
|
|
1319
|
+
for (let j = 1; j < iterations; j++) {
|
|
1320
|
+
U = (0, exports.sha512hmac)(password, U);
|
|
1321
|
+
for (let k = 0; k < hLen; k++)
|
|
1322
|
+
T[k] ^= U[k];
|
|
1323
|
+
}
|
|
1324
|
+
for (let i = 0; i < T.length; i++) {
|
|
1325
|
+
DK[destPos + i] = T[i];
|
|
1326
|
+
}
|
|
1327
|
+
destPos += hLen;
|
|
1328
|
+
}
|
|
1329
|
+
return DK.slice(0, keylen);
|
|
1330
|
+
}
|
|
1331
|
+
exports.pbkdf2 = pbkdf2;
|
|
1332
|
+
//# sourceMappingURL=Hash.js.map
|