...

Source file src/edge-infra.dev/pkg/lib/crypto/osutilcrypt/common/base64.go

Documentation: edge-infra.dev/pkg/lib/crypto/osutilcrypt/common

     1  // Copyright 2012, Jeramey Crawford <jeramey@antihe.ro>
     2  // Copyright 2013, Jonas mg
     3  // All rights reserved.
     4  //
     5  // Use of this source code is governed by a BSD-style license
     6  // that can be found in the LICENSE file.
     7  
     8  package common
     9  
    10  const alphabet = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    11  
    12  func Base64_24Bit(src []byte) (hash []byte) {
    13  	if len(src) == 0 {
    14  		return []byte{} // TODO: return nil
    15  	}
    16  
    17  	hashSize := (len(src) * 8) / 6
    18  	if (len(src) % 6) != 0 {
    19  		hashSize++
    20  	}
    21  	hash = make([]byte, hashSize)
    22  
    23  	dst := hash
    24  	for len(src) > 0 {
    25  		switch len(src) {
    26  		default:
    27  			dst[0] = alphabet[src[0]&0x3f]
    28  			dst[1] = alphabet[((src[0]>>6)|(src[1]<<2))&0x3f]
    29  			dst[2] = alphabet[((src[1]>>4)|(src[2]<<4))&0x3f]
    30  			dst[3] = alphabet[(src[2]>>2)&0x3f]
    31  			src = src[3:]
    32  			dst = dst[4:]
    33  		case 2:
    34  			dst[0] = alphabet[src[0]&0x3f]
    35  			dst[1] = alphabet[((src[0]>>6)|(src[1]<<2))&0x3f]
    36  			dst[2] = alphabet[(src[1]>>4)&0x3f]
    37  			src = src[2:]
    38  			dst = dst[3:]
    39  		case 1:
    40  			dst[0] = alphabet[src[0]&0x3f]
    41  			dst[1] = alphabet[(src[0]>>6)&0x3f]
    42  			src = src[1:]
    43  			dst = dst[2:]
    44  		}
    45  	}
    46  
    47  	return hash
    48  }
    49  

View as plain text