// -*- go -*- // // Copyright (c) 2020-2023 Markku Rossi // // All rights reserved. // // Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and // SHA-512/256 cryptographic hash functions. package sha512 const ( // The size of a SHA512 checksum in bytes. Size = 64 // BlockSize is the block size, in bytes, of the SHA-512/224, // SHA-512/256, SHA-384 and SHA-512 hash functions. BlockSize = 128 init = 0x6a09e667f3bcc908bb67ae8584caa73b3c6ef372fe94f82ba54ff53a5f1d36f1510e527fade682d19b05688c2b3e6c1f1f83d9abfb41bd6b5be0cd19137e2179 ) // Sum512 returns the SHA512 checksum of the data. func Sum512(data []byte) [Size]byte { var state uint512 = init var block uint1024 var hash [Size]byte var pad [BlockSize]byte pad[0] = 0x80 for i := 0; i < len(data); i++ { block <<= 8 block = block | uint1024(data[i]) if (i+1)%BlockSize == 0 { state = Block(block, state) block = 0 } } if len(data)%BlockSize < 112 { for i := len(data) % BlockSize; i < 112; i++ { block <<= 8 block |= uint1024(pad[i-len(data)%BlockSize]) } } else { for i := len(data) % BlockSize; i < BlockSize; i++ { block <<= 8 block |= uint1024(pad[i-len(data)%BlockSize]) } state = Block(block, state) block = 0 } // Length in bits. block <<= 128 block |= uint1024(len(data) << 3) state = Block(block, state) for i := 0; i < Size; i++ { hash[Size-i-1] = byte(state & 0xff) state >>= 8 } return hash } // Block adds a new SHA-512 block to the state. func Block(block uint1024, state uint512) uint512 { return native("sha512.qclc", block, state) }