Skip to content

Commit 016e04b

Browse files
authored
Merge pull request #60 from Findeton/master
Add SHA1 HMAC
2 parents 9329a20 + 25a34f0 commit 016e04b

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

package-lock.json

Lines changed: 4 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cryptography/abstract/HMAC.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Digest } from "./Digest"
2+
/**
3+
* A HMAC ( commonly referred to as a Hash ) (hash-based message authentication
4+
* code) is a type of message authentication code (MAC) that uses a
5+
* cryptographic hash function and a secret key. It may be used to verify data
6+
* integrity and authentication of a message.
7+
*
8+
* Strontium assumes that HMAC functions will run on input that is entirely
9+
* in memory. The interface does not currently support calculations against a
10+
* stream.
11+
*
12+
* The exact nature or security properties of a given HMAC implementation
13+
* will vary based on the implementing class.
14+
*/
15+
export abstract class HMAC extends Digest {
16+
/**
17+
* Create a new HMAC Encrypter.
18+
*
19+
* @param secretKey - The shared secret to use for encryption.
20+
*/
21+
constructor(protected secretKey: Buffer) {
22+
super()
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { HMAC } from "../../abstract/HMAC"
2+
import { createHmac } from "crypto"
3+
4+
/**
5+
* The SHA1HMAC provides a HMAC implementation using SHA1 based on Node's
6+
* OpenSSL.
7+
*
8+
* This implementation relies on the node crypto implementation and may vary based
9+
* on the build flags of the underlying runtime.
10+
*/
11+
export class SHA1HMAC extends HMAC {
12+
public async calculate(input: Buffer): Promise<Buffer> {
13+
let hmacBuilder = createHmac("sha1", this.secretKey)
14+
hmacBuilder.update(input)
15+
16+
return hmacBuilder.digest()
17+
}
18+
}

src/cryptography/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
export { AsymmetricSigner } from "./abstract/AsymmetricSigner"
22
export { Digest } from "./abstract/Digest"
3+
export { HMAC } from "./abstract/HMAC"
34
export { JWTSigner } from "./abstract/JWTSigner"
45
export { MessageAuthenticationCode } from "./abstract/MessageAuthenticationCode"
56
export { SymmetricEncrypter } from "./abstract/SymmetricEncrypter"
67

78
export { AsymmetricJWTSigner } from "./drivers/node/AsymmetricJWTSigner"
89
export { RSASHA256Signer } from "./drivers/node/RSASHA256Signer"
910
export { SHA256Digest } from "./drivers/node/SHA256Digest"
11+
export { SHA1HMAC } from "./drivers/node/SHA1HMAC"

0 commit comments

Comments
 (0)