|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Functional Streams for Scala |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package fs2 |
| 23 | +package hashing |
| 24 | + |
| 25 | +import cats.effect.{Resource, Sync} |
| 26 | + |
| 27 | +import java.security.MessageDigest |
| 28 | +import javax.crypto.Mac |
| 29 | +import javax.crypto.spec.SecretKeySpec |
| 30 | + |
| 31 | +private[hashing] trait HasherCompanionPlatform { |
| 32 | + |
| 33 | + private[hashing] def apply[F[_]: Sync](algorithm: HashAlgorithm): Resource[F, Hasher[F]] = |
| 34 | + Resource.eval(Sync[F].delay(unsafe(algorithm))) |
| 35 | + |
| 36 | + private[hashing] def hmac[F[_]: Sync]( |
| 37 | + algorithm: HashAlgorithm, |
| 38 | + key: Chunk[Byte] |
| 39 | + ): Resource[F, Hasher[F]] = |
| 40 | + Resource.eval(Sync[F].delay(unsafeHmac(algorithm, key))) |
| 41 | + |
| 42 | + private[hashing] def unsafe[F[_]: Sync](algorithm: HashAlgorithm): Hasher[F] = |
| 43 | + unsafeFromMessageDigest(MessageDigest.getInstance(toAlgorithmString(algorithm))) |
| 44 | + |
| 45 | + private[hashing] def toAlgorithmString(algorithm: HashAlgorithm): String = |
| 46 | + algorithm match { |
| 47 | + case HashAlgorithm.MD5 => "MD5" |
| 48 | + case HashAlgorithm.SHA1 => "SHA-1" |
| 49 | + case HashAlgorithm.SHA224 => "SHA-224" |
| 50 | + case HashAlgorithm.SHA256 => "SHA-256" |
| 51 | + case HashAlgorithm.SHA384 => "SHA-384" |
| 52 | + case HashAlgorithm.SHA512 => "SHA-512" |
| 53 | + case HashAlgorithm.SHA512_224 => "SHA-512/224" |
| 54 | + case HashAlgorithm.SHA512_256 => "SHA-512/256" |
| 55 | + case HashAlgorithm.SHA3_224 => "SHA3-224" |
| 56 | + case HashAlgorithm.SHA3_256 => "SHA3-256" |
| 57 | + case HashAlgorithm.SHA3_384 => "SHA3-384" |
| 58 | + case HashAlgorithm.SHA3_512 => "SHA3-512" |
| 59 | + case HashAlgorithm.Named(name) => name |
| 60 | + case other => sys.error(s"unsupported algorithm $other") |
| 61 | + } |
| 62 | + |
| 63 | + private[hashing] def unsafeHmac[F[_]: Sync]( |
| 64 | + algorithm: HashAlgorithm, |
| 65 | + key: Chunk[Byte] |
| 66 | + ): Hasher[F] = { |
| 67 | + val name = toMacAlgorithmString(algorithm) |
| 68 | + val mac = Mac.getInstance(name) |
| 69 | + mac.init(new SecretKeySpec(key.toArray, name)) |
| 70 | + unsafeFromMac(mac) |
| 71 | + } |
| 72 | + |
| 73 | + private[hashing] def toMacAlgorithmString(algorithm: HashAlgorithm): String = |
| 74 | + algorithm match { |
| 75 | + case HashAlgorithm.MD5 => "HmacMD5" |
| 76 | + case HashAlgorithm.SHA1 => "HmacSHA1" |
| 77 | + case HashAlgorithm.SHA224 => "HmacSHA224" |
| 78 | + case HashAlgorithm.SHA256 => "HmacSHA256" |
| 79 | + case HashAlgorithm.SHA384 => "HmacSHA384" |
| 80 | + case HashAlgorithm.SHA512 => "HmacSHA512" |
| 81 | + case HashAlgorithm.SHA512_224 => "HmacSHA512/224" |
| 82 | + case HashAlgorithm.SHA512_256 => "HmacSHA512/256" |
| 83 | + case HashAlgorithm.SHA3_224 => "HmacSHA3-224" |
| 84 | + case HashAlgorithm.SHA3_256 => "HmacSHA3-256" |
| 85 | + case HashAlgorithm.SHA3_384 => "HmacSHA3-384" |
| 86 | + case HashAlgorithm.SHA3_512 => "HmacSHA3-512" |
| 87 | + case HashAlgorithm.Named(name) => name |
| 88 | + case other => sys.error(s"unsupported algorithm $other") |
| 89 | + } |
| 90 | + |
| 91 | + def unsafeFromMessageDigest[F[_]: Sync](d: MessageDigest): Hasher[F] = |
| 92 | + new SyncHasher[F] { |
| 93 | + def unsafeUpdate(chunk: Chunk[Byte]): Unit = { |
| 94 | + val slice = chunk.toArraySlice |
| 95 | + d.update(slice.values, slice.offset, slice.size) |
| 96 | + } |
| 97 | + |
| 98 | + def unsafeHash(): Hash = |
| 99 | + Hash(Chunk.array(d.digest())) |
| 100 | + } |
| 101 | + |
| 102 | + def unsafeFromMac[F[_]: Sync](d: Mac): Hasher[F] = |
| 103 | + new SyncHasher[F] { |
| 104 | + def unsafeUpdate(chunk: Chunk[Byte]): Unit = { |
| 105 | + val slice = chunk.toArraySlice |
| 106 | + d.update(slice.values, slice.offset, slice.size) |
| 107 | + } |
| 108 | + |
| 109 | + def unsafeHash(): Hash = |
| 110 | + Hash(Chunk.array(d.doFinal())) |
| 111 | + } |
| 112 | +} |
0 commit comments