Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison src/main/kotlin/name/blackcap/passman/Hashing.kt @ 0:a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 11 Sep 2022 16:11:37 -0700 |
parents | |
children | 698c4a3d758d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a6cfdffcaa94 |
---|---|
1 package name.blackcap.passman | |
2 | |
3 import java.nio.ByteBuffer | |
4 import java.nio.ByteOrder | |
5 import java.nio.CharBuffer | |
6 import java.nio.charset.Charset | |
7 import java.nio.charset.StandardCharsets | |
8 import java.security.MessageDigest | |
9 | |
10 object Hashing { | |
11 private const val HASHING_ALGORITHM = "MD5" | |
12 private val CHARSET: Charset = StandardCharsets.UTF_8 | |
13 | |
14 fun hash(bytes: ByteBuffer): Long { | |
15 val md = MessageDigest.getInstance(HASHING_ALGORITHM) | |
16 bytes.rewind() | |
17 md.update(bytes) | |
18 return toLong(md.digest()) | |
19 } | |
20 | |
21 fun hash(bytes: ByteArray): Long = | |
22 hash(ByteBuffer.wrap(bytes)) | |
23 | |
24 fun hash(chars: CharArray): Long = | |
25 hash(CHARSET.encode(CharBuffer.wrap(chars))) | |
26 | |
27 fun hash(string: String): Long = | |
28 hash(CHARSET.encode(string)) | |
29 | |
30 private fun toLong(hash: ByteArray): Long = ByteBuffer.wrap(hash).run { | |
31 order(ByteOrder.LITTLE_ENDIAN) | |
32 getLong(hash.size - 8) | |
33 } | |
34 } |