| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import java.nio.charset.StandardCharsets; | |
| 4 | import java.util.Base64; | |
| 5 | import javax.crypto.SecretKey; | |
| 6 | import javax.crypto.spec.SecretKeySpec; | |
| 7 | import org.springframework.beans.factory.annotation.Value; | |
| 8 | import org.springframework.security.crypto.codec.Hex; | |
| 9 | import org.springframework.security.crypto.encrypt.AesBytesEncryptor; | |
| 10 | import org.springframework.security.crypto.keygen.KeyGenerators; | |
| 11 | import org.springframework.stereotype.Service; | |
| 12 | ||
| 13 | @Service | |
| 14 | public class TokenEncryptionService { | |
| 15 | ||
| 16 | private final AesBytesEncryptor encryptor; | |
| 17 | ||
| 18 | public TokenEncryptionService(@Value("${app.secret}") String secret) { | |
| 19 | byte[] keyBytes = Hex.decode(secret); | |
| 20 | SecretKey key = new SecretKeySpec(keyBytes, "AES"); | |
| 21 | this.encryptor = | |
| 22 | new AesBytesEncryptor( | |
| 23 | key, KeyGenerators.secureRandom(12), AesBytesEncryptor.CipherAlgorithm.GCM); | |
| 24 | } | |
| 25 | ||
| 26 | public String encryptToken(String token) { | |
| 27 |
3
1. encryptToken : replaced return value with "" for edu/ucsb/cs156/frontiers/services/TokenEncryptionService::encryptToken → KILLED 2. encryptToken : negated conditional → KILLED 3. encryptToken : negated conditional → KILLED |
if (token == null || token.isEmpty()) return null; |
| 28 | byte[] encrypted = encryptor.encrypt(token.getBytes(StandardCharsets.UTF_8)); | |
| 29 |
1
1. encryptToken : replaced return value with "" for edu/ucsb/cs156/frontiers/services/TokenEncryptionService::encryptToken → KILLED |
return Base64.getEncoder().encodeToString(encrypted); |
| 30 | } | |
| 31 | ||
| 32 | public String decryptToken(String encryptedToken) { | |
| 33 |
3
1. decryptToken : replaced return value with "" for edu/ucsb/cs156/frontiers/services/TokenEncryptionService::decryptToken → NO_COVERAGE 2. decryptToken : negated conditional → KILLED 3. decryptToken : negated conditional → KILLED |
if (encryptedToken == null || encryptedToken.isEmpty()) return null; |
| 34 | byte[] decrypted = encryptor.decrypt(Base64.getDecoder().decode(encryptedToken)); | |
| 35 |
1
1. decryptToken : replaced return value with "" for edu/ucsb/cs156/frontiers/services/TokenEncryptionService::decryptToken → KILLED |
return new String(decrypted, StandardCharsets.UTF_8); |
| 36 | } | |
| 37 | } | |
Mutations | ||
| 27 |
1.1 2.2 3.3 |
|
| 29 |
1.1 |
|
| 33 |
1.1 2.2 3.3 |
|
| 35 |
1.1 |