How to use sha384 method of crypto Package

Best K6 code snippet using crypto.sha384

sha512.go

Source:sha512.go Github

copy

Full Screen

1// Copyright 2009 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4// Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/2565// hash algorithms as defined in FIPS 180-4.6//7// All the hash.Hash implementations returned by this package also8// implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to9// marshal and unmarshal the internal state of the hash.10package sha51211import (12 "crypto"13 "errors"14 "hash"15)16func init() {17 crypto.RegisterHash(crypto.SHA384, New384)18 crypto.RegisterHash(crypto.SHA512, New)19 crypto.RegisterHash(crypto.SHA512_224, New512_224)20 crypto.RegisterHash(crypto.SHA512_256, New512_256)21}22const (23 // Size is the size, in bytes, of a SHA-512 checksum.24 Size = 6425 // Size224 is the size, in bytes, of a SHA-512/224 checksum.26 Size224 = 2827 // Size256 is the size, in bytes, of a SHA-512/256 checksum.28 Size256 = 3229 // Size384 is the size, in bytes, of a SHA-384 checksum.30 Size384 = 4831 // BlockSize is the block size, in bytes, of the SHA-512/224,32 // SHA-512/256, SHA-384 and SHA-512 hash functions.33 BlockSize = 12834)35const (36 chunk = 12837 init0 = 0x6a09e667f3bcc90838 init1 = 0xbb67ae8584caa73b39 init2 = 0x3c6ef372fe94f82b40 init3 = 0xa54ff53a5f1d36f141 init4 = 0x510e527fade682d142 init5 = 0x9b05688c2b3e6c1f43 init6 = 0x1f83d9abfb41bd6b44 init7 = 0x5be0cd19137e217945 init0_224 = 0x8c3d37c819544da246 init1_224 = 0x73e1996689dcd4d647 init2_224 = 0x1dfab7ae32ff9c8248 init3_224 = 0x679dd514582f9fcf49 init4_224 = 0x0f6d2b697bd44da850 init5_224 = 0x77e36f7304c4894251 init6_224 = 0x3f9d85a86a1d36c852 init7_224 = 0x1112e6ad91d692a153 init0_256 = 0x22312194fc2bf72c54 init1_256 = 0x9f555fa3c84c64c255 init2_256 = 0x2393b86b6f53b15156 init3_256 = 0x963877195940eabd57 init4_256 = 0x96283ee2a88effe358 init5_256 = 0xbe5e1e255386399259 init6_256 = 0x2b0199fc2c85b8aa60 init7_256 = 0x0eb72ddc81c52ca261 init0_384 = 0xcbbb9d5dc1059ed862 init1_384 = 0x629a292a367cd50763 init2_384 = 0x9159015a3070dd1764 init3_384 = 0x152fecd8f70e593965 init4_384 = 0x67332667ffc00b3166 init5_384 = 0x8eb44a876858151167 init6_384 = 0xdb0c2e0d64f98fa768 init7_384 = 0x47b5481dbefa4fa469)70// digest represents the partial evaluation of a checksum.71type digest struct {72 h [8]uint6473 x [chunk]byte74 nx int75 len uint6476 function crypto.Hash77}78func (d *digest) Reset() {79 switch d.function {80 case crypto.SHA384:81 d.h[0] = init0_38482 d.h[1] = init1_38483 d.h[2] = init2_38484 d.h[3] = init3_38485 d.h[4] = init4_38486 d.h[5] = init5_38487 d.h[6] = init6_38488 d.h[7] = init7_38489 case crypto.SHA512_224:90 d.h[0] = init0_22491 d.h[1] = init1_22492 d.h[2] = init2_22493 d.h[3] = init3_22494 d.h[4] = init4_22495 d.h[5] = init5_22496 d.h[6] = init6_22497 d.h[7] = init7_22498 case crypto.SHA512_256:99 d.h[0] = init0_256100 d.h[1] = init1_256101 d.h[2] = init2_256102 d.h[3] = init3_256103 d.h[4] = init4_256104 d.h[5] = init5_256105 d.h[6] = init6_256106 d.h[7] = init7_256107 default:108 d.h[0] = init0109 d.h[1] = init1110 d.h[2] = init2111 d.h[3] = init3112 d.h[4] = init4113 d.h[5] = init5114 d.h[6] = init6115 d.h[7] = init7116 }117 d.nx = 0118 d.len = 0119}120const (121 magic384 = "sha\x04"122 magic512_224 = "sha\x05"123 magic512_256 = "sha\x06"124 magic512 = "sha\x07"125 marshaledSize = len(magic512) + 8*8 + chunk + 8126)127func (d *digest) MarshalBinary() ([]byte, error) {128 b := make([]byte, 0, marshaledSize)129 switch d.function {130 case crypto.SHA384:131 b = append(b, magic384...)132 case crypto.SHA512_224:133 b = append(b, magic512_224...)134 case crypto.SHA512_256:135 b = append(b, magic512_256...)136 case crypto.SHA512:137 b = append(b, magic512...)138 default:139 return nil, errors.New("crypto/sha512: invalid hash function")140 }141 b = appendUint64(b, d.h[0])142 b = appendUint64(b, d.h[1])143 b = appendUint64(b, d.h[2])144 b = appendUint64(b, d.h[3])145 b = appendUint64(b, d.h[4])146 b = appendUint64(b, d.h[5])147 b = appendUint64(b, d.h[6])148 b = appendUint64(b, d.h[7])149 b = append(b, d.x[:d.nx]...)150 b = b[:len(b)+len(d.x)-int(d.nx)] // already zero151 b = appendUint64(b, d.len)152 return b, nil153}154func (d *digest) UnmarshalBinary(b []byte) error {155 if len(b) < len(magic512) {156 return errors.New("crypto/sha512: invalid hash state identifier")157 }158 switch {159 case d.function == crypto.SHA384 && string(b[:len(magic384)]) == magic384:160 case d.function == crypto.SHA512_224 && string(b[:len(magic512_224)]) == magic512_224:161 case d.function == crypto.SHA512_256 && string(b[:len(magic512_256)]) == magic512_256:162 case d.function == crypto.SHA512 && string(b[:len(magic512)]) == magic512:163 default:164 return errors.New("crypto/sha512: invalid hash state identifier")165 }166 if len(b) != marshaledSize {167 return errors.New("crypto/sha512: invalid hash state size")168 }169 b = b[len(magic512):]170 b, d.h[0] = consumeUint64(b)171 b, d.h[1] = consumeUint64(b)172 b, d.h[2] = consumeUint64(b)173 b, d.h[3] = consumeUint64(b)174 b, d.h[4] = consumeUint64(b)175 b, d.h[5] = consumeUint64(b)176 b, d.h[6] = consumeUint64(b)177 b, d.h[7] = consumeUint64(b)178 b = b[copy(d.x[:], b):]179 b, d.len = consumeUint64(b)180 d.nx = int(d.len) % chunk181 return nil182}183func appendUint64(b []byte, x uint64) []byte {184 a := [8]byte{185 byte(x >> 56),186 byte(x >> 48),187 byte(x >> 40),188 byte(x >> 32),189 byte(x >> 24),190 byte(x >> 16),191 byte(x >> 8),192 byte(x),193 }194 return append(b, a[:]...)195}196func consumeUint64(b []byte) ([]byte, uint64) {197 _ = b[7]198 x := uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |199 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56200 return b[8:], x201}202// New returns a new hash.Hash computing the SHA-512 checksum.203func New() hash.Hash {204 d := &digest{function: crypto.SHA512}205 d.Reset()206 return d207}208// New512_224 returns a new hash.Hash computing the SHA-512/224 checksum.209func New512_224() hash.Hash {210 d := &digest{function: crypto.SHA512_224}211 d.Reset()212 return d213}214// New512_256 returns a new hash.Hash computing the SHA-512/256 checksum.215func New512_256() hash.Hash {216 d := &digest{function: crypto.SHA512_256}217 d.Reset()218 return d219}220// New384 returns a new hash.Hash computing the SHA-384 checksum.221func New384() hash.Hash {222 d := &digest{function: crypto.SHA384}223 d.Reset()224 return d225}226func (d *digest) Size() int {227 switch d.function {228 case crypto.SHA512_224:229 return Size224230 case crypto.SHA512_256:231 return Size256232 case crypto.SHA384:233 return Size384234 default:235 return Size236 }237}238func (d *digest) BlockSize() int { return BlockSize }239func (d *digest) Write(p []byte) (nn int, err error) {240 nn = len(p)241 d.len += uint64(nn)242 if d.nx > 0 {243 n := copy(d.x[d.nx:], p)244 d.nx += n245 if d.nx == chunk {246 block(d, d.x[:])247 d.nx = 0248 }249 p = p[n:]250 }251 if len(p) >= chunk {252 n := len(p) &^ (chunk - 1)253 block(d, p[:n])254 p = p[n:]255 }256 if len(p) > 0 {257 d.nx = copy(d.x[:], p)258 }259 return260}261func (d0 *digest) Sum(in []byte) []byte {262 // Make a copy of d0 so that caller can keep writing and summing.263 d := new(digest)264 *d = *d0265 hash := d.checkSum()266 switch d.function {267 case crypto.SHA384:268 return append(in, hash[:Size384]...)269 case crypto.SHA512_224:270 return append(in, hash[:Size224]...)271 case crypto.SHA512_256:272 return append(in, hash[:Size256]...)273 default:274 return append(in, hash[:]...)275 }276}277func (d *digest) checkSum() [Size]byte {278 // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.279 len := d.len280 var tmp [128]byte281 tmp[0] = 0x80282 if len%128 < 112 {283 d.Write(tmp[0 : 112-len%128])284 } else {285 d.Write(tmp[0 : 128+112-len%128])286 }287 // Length in bits.288 len <<= 3289 for i := uint(0); i < 16; i++ {290 tmp[i] = byte(len >> (120 - 8*i))291 }292 d.Write(tmp[0:16])293 if d.nx != 0 {294 panic("d.nx != 0")295 }296 h := d.h[:]297 if d.function == crypto.SHA384 {298 h = d.h[:6]299 }300 var digest [Size]byte301 for i, s := range h {302 digest[i*8] = byte(s >> 56)303 digest[i*8+1] = byte(s >> 48)304 digest[i*8+2] = byte(s >> 40)305 digest[i*8+3] = byte(s >> 32)306 digest[i*8+4] = byte(s >> 24)307 digest[i*8+5] = byte(s >> 16)308 digest[i*8+6] = byte(s >> 8)309 digest[i*8+7] = byte(s)310 }311 return digest312}313// Sum512 returns the SHA512 checksum of the data.314func Sum512(data []byte) [Size]byte {315 d := digest{function: crypto.SHA512}316 d.Reset()317 d.Write(data)318 return d.checkSum()319}320// Sum384 returns the SHA384 checksum of the data.321func Sum384(data []byte) (sum384 [Size384]byte) {322 d := digest{function: crypto.SHA384}323 d.Reset()324 d.Write(data)325 sum := d.checkSum()326 copy(sum384[:], sum[:Size384])327 return328}329// Sum512_224 returns the Sum512/224 checksum of the data.330func Sum512_224(data []byte) (sum224 [Size224]byte) {331 d := digest{function: crypto.SHA512_224}332 d.Reset()333 d.Write(data)334 sum := d.checkSum()335 copy(sum224[:], sum[:Size224])336 return337}338// Sum512_256 returns the Sum512/256 checksum of the data.339func Sum512_256(data []byte) (sum256 [Size256]byte) {340 d := digest{function: crypto.SHA512_256}341 d.Reset()342 d.Write(data)343 sum := d.checkSum()344 copy(sum256[:], sum[:Size256])345 return346}...

Full Screen

Full Screen

hash.go

Source:hash.go Github

copy

Full Screen

1package libtrust2import (3 "crypto"4 _ "crypto/sha256" // Registrer SHA224 and SHA2565 _ "crypto/sha512" // Registrer SHA384 and SHA5126 "fmt"7)8type signatureAlgorithm struct {9 algHeaderParam string10 hashID crypto.Hash11}12func (h *signatureAlgorithm) HeaderParam() string {13 return h.algHeaderParam14}15func (h *signatureAlgorithm) HashID() crypto.Hash {16 return h.hashID17}18var (19 rs256 = &signatureAlgorithm{"RS256", crypto.SHA256}20 rs384 = &signatureAlgorithm{"RS384", crypto.SHA384}21 rs512 = &signatureAlgorithm{"RS512", crypto.SHA512}22 es256 = &signatureAlgorithm{"ES256", crypto.SHA256}23 es384 = &signatureAlgorithm{"ES384", crypto.SHA384}24 es512 = &signatureAlgorithm{"ES512", crypto.SHA512}25)26func rsaSignatureAlgorithmByName(alg string) (*signatureAlgorithm, error) {27 switch {28 case alg == "RS256":29 return rs256, nil30 case alg == "RS384":31 return rs384, nil32 case alg == "RS512":33 return rs512, nil34 default:35 return nil, fmt.Errorf("RSA Digital Signature Algorithm %q not supported", alg)36 }37}38func rsaPKCS1v15SignatureAlgorithmForHashID(hashID crypto.Hash) *signatureAlgorithm {39 switch {40 case hashID == crypto.SHA512:41 return rs51242 case hashID == crypto.SHA384:43 return rs38444 case hashID == crypto.SHA256:45 fallthrough46 default:47 return rs25648 }49}...

Full Screen

Full Screen

sha384

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := sha512.New()4 h.Write([]byte(s))5 bs := h.Sum(nil)6 fmt.Println(s)7 fmt.Printf("%x8}

Full Screen

Full Screen

sha384

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := sha384.New()4 io.WriteString(h, "test")5 fmt.Printf("%x", h.Sum(nil))6}

Full Screen

Full Screen

sha384

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Sha384")4 h := sha512.New384()5 h.Write([]byte(s))6 bs := h.Sum(nil)7 fmt.Println(s)

Full Screen

Full Screen

sha384

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := sha512.New()4 h.Write([]byte(s))5 bs := h.Sum(nil)6 h2 := sha512.New()7 h2.Write([]byte(p))8 ps := h2.Sum(nil)9 fmt.Println("Does the original string contain the pattern?")10 fmt.Printf("%t", sha512.Sum512([]byte(s)) == sha512.Sum512([]byte(p)))11}

Full Screen

Full Screen

sha384

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := sha256.New()4 h.Write([]byte(str))5 bs := h.Sum(nil)6 fmt.Println(str)7 fmt.Printf("%x8 h384 := sha512.New384()9 h384.Write([]byte(str))10 bs384 := h384.Sum(nil)11 fmt.Println(str)12 fmt.Printf("%x13 h512 := sha512.New()14 h512.Write([]byte(str))

Full Screen

Full Screen

sha384

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("sha384")4 h := sha512.New384()5 h.Write([]byte("hello"))6 fmt.Printf("%x7", h.Sum(nil))8}9}10import (11func main() {12 fmt.Println("sha512")13 h := sha512.New()14 h.Write([]byte("hello"))15 fmt.Printf("%x16", h.Sum(nil))17}18}19import (20func main() {21 fmt.Println("sha512_224")22 h := sha512.New512_224()23 h.Write([]byte("hello"))24 fmt.Printf("%x25", h.Sum(nil))26}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful