๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“– Java&Spring

Java์˜ Hash ์•Œ๊ณ ๋ฆฌ์ฆ˜ MessageDigest

์š”์ฆ˜ ํ•ซํ•œ ๋ณด์•ˆ ๋ฌธ์ œ โ—

์ด๋ฒˆ์— ์‹ ๊ทœ ๊ฐœ๋ฐœํ•˜๋Š” ๊ธฐ๋Šฅ์—๋„ ๋ณด์•ˆ์„ ์œ„ํ•ด ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๋‚ด์šฉ์€ ์•”ํ˜ธํ™”๋ฅผ ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ต๋‹ˆ๋‹ค.

Java์—์„œ Hash ๊ฐ’ ์ƒ์„ฑ์„ ํ•  ๋•Œ ๋งŽ์ด ์“ฐ๋Š” ํด๋ž˜์Šค๋Š” MessageDigest์ธ๋ฐ, ์˜ค๋Š˜์€ ์ด ์นœ๊ตฌ๋ฅผ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ”MessageDigest

 

https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html

 

MessageDigest (Java Platform SE 7 )

This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value. A MessageDigest

docs.oracle.com

 

๊ณต์‹ API ๋ฌธ์„œ ๋‚ด์šฉ์„ ์ธ์šฉํ•˜์ž๋ฉด~

MessageDigest๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ SHA-1์ด๋‚˜ SHA-256 ๊ฐ™์€ digest ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด ์ค๋‹ˆ๋‹ค.

(๊ทธ๋ž˜์„œ ํด๋ž˜์Šค ์ด๋ฆ„์ด ์ด๋žฌ๊ตฐ์š”)

์ž„์˜ ํฌ๊ธฐ(arbitrary-sized)๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , one-way ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์„œ, ๊ณ ์ • ๊ธธ์ด์˜ ํ•ด์‹œ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

 

MessageDigest md = MessageDigest.getInstance("SHA-256");

์„ ์–ธ์€ ์œ„์— ์˜ˆ์‹œ์ฒ˜๋Ÿผ ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ, MessageDigest object๋Š” ์ดˆ๊ธฐํ™”๋œ ์ƒํƒœ๋กœ ์‹œ์ž‘์ด ๋ฉ๋‹ˆ๋‹ค.

 

๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์ค‘์š”ํ•œ ํ•จ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋‹ต๋‹ˆ๋‹ค.

 

๐Ÿ”‘ getInstance("SHA-256")

 

md.getInstancd("SHA-256");

getInstance() ํŒŒ๋ผ๋ฏธํ„ฐ์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฆ„์„ ์ ์–ด์ฃผ๋ฉด, MessageDigest Object๊ฐ€ ์ƒ์„ฑ ๋ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฆ„์ด ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ฐฉ์‹์ด๋ผ๋ฉด Exception์œผ๋กœ NoSuchAlgorithmException๊ฐ€ ์˜ค๊ธฐ ๋•Œ๋ฌธ์—, try-catch๋กœ ์ฒ˜๋ฆฌํ•ด์ค์‹œ๋‹ค.
๊ตฌํ˜„ ๊ฐ€๋Šฅํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ข…๋ฅ˜๋Š” ์•„๋ž˜ ์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest

 

Standard Algorithm Name Documentation

SSL3Padding The padding scheme defined in the SSL Protocol Version 3.0, November 18, 1996, section 5.2.3.2 (CBC block cipher): block-ciphered struct { opaque content[SSLCompressed.length]; opaque MAC[CipherSpec.hash_size]; uint8 padding[ GenericBlockCipher

docs.oracle.com

 

๐Ÿ”‘ update()

 

md.update(("์•”ํ˜ธํ™”ํ•  ๋ฌธ์ž์—ด").getBytes())

update()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด์‹œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ ์žํ•˜๋Š” ๋ฌธ์ž์—ด์„ MessageDigest์— update ํ•ด์ค๋‹ˆ๋‹ค.
update()๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค MessageDigest object์— ์ €์žฅ๋œ digest ๊ฐ’์ด ๊ฐฑ์‹ ์ด ๋ฉ๋‹ˆ๋‹ค
(reset() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ดˆ๊ธฐํ™” ํ•ด์ค„ ์ˆ˜๋„ ์žˆ์Œ)

 

๐Ÿ”‘ digest()

 

byte[] hashValue = md.digest();

update๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์‹œ ๊ฐ’์œผ๋กœ ์™„์„ฑํ•ด์ค๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ๊ฐ’์€ toString()์œผ๋กœ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค.

'๐Ÿ“– Java&Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๐ŸŒธ startsWith()/endWith() ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘๊ณผ ๋  (0) 2023.04.04
@JsonProperty์™€ @JsonNaming  (0) 2023.03.17
HTTP Query Parameter์—์„œ Arrary List ์‚ฌ์šฉ  (0) 2023.02.01
Spring Cron Expression  (0) 2023.01.25
StringUtils  (0) 2023.01.18